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


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

ls | % Name > file_names.txt

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

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.txt

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

Example: How to List All Files in Directory to Text 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:

  • my_data.txt
  • new_data.txt
  • old_data.txt
  • that_data.txt
  • this_data.txt

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

We can use the following syntax to do so:

ls | % Name > file_names.txt

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

PowerShell list files in directory to text file

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

We can see that all of the file names from the directory are listed in the text 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:

PowerShell list files in directory to text file without extensions

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

We can see that all of the file names from the directory are listed in the text 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