PowerShell: How to List All Files in Directory to CSV File


You can use the following syntax in PowerShell to list all files in a directory to a CSV file:

ls | % Name > file_names.csv

This particular example will list out all file names in the current directory to a CSV file named file_names.csv.

Note that this syntax will list all file names with the extensions.

If you would instead like to list all file names without the extensions, you could use the following syntax instead:

ls | % Basename > file_names.csv

The following example shows how to use this syntax in practice.

Example: How to List All Files in Directory to CSV File Using PowerShell

Suppose that our current directory has the following path:

  • C:\Users\bobbi\OneDrive\Documents\test_data

We can type Dir to list out all files in this current directory:

We can see that there are five total files in the directory:

  • cool_data.xlsx
  • my_data.txt
  • new_data.txt
  • that_data.txt
  • this_data.txt

Suppose that we we would like to list out all of these file names to a CSV file.

We can use the following syntax to do so:

ls | % Name > file_names.csv

The following screenshot shows how to use this syntax in practice:

PowerShell list all files in directory to CSV

We can then navigate to this CSV file on our computer and view it.

For example, here is what the file looks like if we open it in Notepad:

We can see that all of the file names from the directory are listed in the CSV file.

If we would instead like to list all of the file names without the extensions, we can use the following syntax to do so:

We can then navigate to this CSV file on our computer and view it:

We can see that all of the file names from the directory are listed in the CSV file without their extensions.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Delete All Files with Specific Extension
PowerShell: How to Rename File Extension of Multiple Files
PowerShell: How to Replace Every Occurrence of String in File

Leave a Reply