Recently, I got a requirement to list down all files with a particular extension across multiple directories on our company’s file servers. It is easy to achieve this using PowerShell. In this tutorial, I will explain how to find all files with a specific extension using PowerShell with examples.
Find All Files with a Specific Extension Using PowerShell from a Single Directory
In PowerShell, we can use the Get-ChildItem cmdlet to find files with specific extensions. This cmdlet allows you to retrieve files and directories from a specified location.
To find all files with a specific extension in a single directory, you can use the following PowerShell command:
Get-ChildItem -Path "C:\MyFolder" -Filter "*.txt" -FileIn this example:
-Pathspecifies the directory where you want to search for files. Replace “C:\MyFolder” with the actual path to your directory.-Filterallows you to specify the file extension you’re looking for. In this case, “*.txt” will match all files with the .txt extension.-Fileensures that only files are returned, excluding directories from the results.
I executed the above PowerShell script; you can see the exact output in the screenshot below.

Check out Find and Remove Empty Folders Using PowerShell
Search for Files Recursively in Subdirectories using PowerShell
If you need to search for files with a specific extension in a directory and all its subdirectories, you can use the -Recurse parameter. Here’s an example:
Get-ChildItem -Path "C:\MyFolder" -Filter "*.pdf" -Recurse -FileThis command will search for all files with the .pdf extension in the “C:\MyFolder” directory and all its subdirectories recursively.
Save the Results to a File
To save the list of files with a specific extension to a file, you can use the Out-File cmdlet. Here’s an example:
Get-ChildItem -Path "C:\MyFolder" -Filter "*.docx" -Recurse -File | Out-File -FilePath "C:\temp\docx_files.txt"This command will search for all files with the .docx extension in the “C:\MyFolder” directory and its subdirectories, and then save the list of files to a text file named “docx_files.txt” in the “C:\temp” directory.
Read PowerShell Copy-item Create Folder If Not Exist
Find All Files with Multiple Extensions Using PowerShell
If you need to find files with multiple extensions, you can use the -Include parameter followed by an array of extensions. Here’s an example:
Get-ChildItem -Path "C:\MyFolder" -Include "*.jpg","*.png","*.gif" -Recurse -FileThis command will search for files with .jpg, .png, and .gif extensions in the specified directory and its subdirectories.
Search for Files with Multiple Extensions on Multiple Servers using PowerShell
You may need to search for files across multiple servers in a corporate environment. PowerShell allows you to perform remote file searches using the -ComputerName parameter. Here’s an example:
Get-ChildItem -Path "C:\SharedData" -Filter "*.xlsx" -Recurse -File -ComputerName "Server01","Server02","Server03"This command will search for files with the .xlsx extension in the “C:\SharedData” directory and its subdirectories on the specified servers: Server01, Server02, and Server03. Make sure you have the necessary permissions to access the remote servers.
Check out How to Use PowerShell to Find Files Modified After a Certain Date?
Export Results to CSV
To export the file search results to a CSV file for further analysis or reporting, you can use the Export-Csv cmdlet. Here’s an example:
Get-ChildItem -Path "C:\MyFolder" -Filter "*.pptx" -Recurse -File | Select-Object FullName,LastWriteTime,Length | Export-Csv -Path "C:\temp\pptx_files.csv" -NoTypeInformationThis command searches for files with the .pptx extension, selects the FullName, LastWriteTime, and Length properties, and then exports the results to a CSV file named “pptx_files.csv” in the “C:\temp” directory. The -NoTypeInformation parameter is used to exclude the type information header from the CSV file.
Handle Large Number of Files
When dealing with a large number of files, it’s essential to consider performance and resource utilization. PowerShell provides the -ReadCount parameter to process files in batches, reducing memory usage. Here’s an example:
Get-ChildItem -Path "C:\MyFolder" -Filter "*.txt" -Recurse -File -ReadCount 1000 | Out-File -FilePath "C:\temp\txt_files.txt"This command processes files in batches of 1000, reducing memory usage and improving performance when searching through a large number of files.
Conclusion
In this tutorial, I explained how to find files with specific extensions using the Get-ChildItem cmdlet in PowerShell. Here, I have explained a few examples related to this.
You may also like:
- How to Use PowerShell new-item to Create Files and Directories?
- How to Sort Files by Date in PowerShell?
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.