PowerShell: How to Find All Files with Specific Extension


You can use the following basic syntax to find all files with a specific extension in a directory using PowerShell:

Get-ChildItem -Filter *.txt -Recurse -File

This particular example will find all files in the current directory that contain .txt as the file extension.

Note that we use -Recurse to specify that we’d like to recursively search through each subfolder in the current directory and we use -File to specify that we only want to return files.

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

Example: How to Find All Files with Specific Extension in PowerShell

Suppose that our current directory has the following path:

  • C:\users\bobbi\data3

We can use the Dir command to list out all files in this current directory:

We can see that there are three files with .txt extensions and two files with .csv extensions.

Suppose that we we would like to retrieve only the files that have .txt extensions.

We can use the following syntax to do so:

Get-ChildItem -Filter *.txt -Recurse -File 

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

PowerShell find all files with extension

We can see that we were able to successfully retrieve only the files that had a .txt extension in the directory.

Note that we could also append -Name to the end of the command to only retrieve the file names instead of the extra information:

Get-ChildItem -Filter *.txt -Recurse -File -Name

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

PowerShell find all files with extension and return name only

We could also use the following syntax to retrieve each file name without the extension:

Get-ChildItem -Filter *.txt -Recurse -File | % { $_.BaseName }

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

PowerShell get base name of each file with specific extension

This returns each of the files that have a .txt extension but does not include the extension in the output.

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 Get File Extension

Leave a Reply