Getting Files with Specific Extensions Only

by Aug 15, 2014

All PowerShell versions

When you use Get-ChildItem to get a list of files, you may have noticed that the -Filter parameter occasionally returns more files than you’ve expected.

Here is an example of this. This line does not just return files with a ".ps1" extension, but also files with the ".ps1xml" file extension:

Get-ChildItem -Path C:\windows -Recurse -ErrorAction SilentlyContinue -Filter *.ps1

To limit the result set to just the extension you are after, add a cmdlet filter to refine the results:

Get-ChildItem -Path C:\windows -Recurse -ErrorAction SilentlyContinue -Filter *.ps1 |
  Where-Object { $_.Extension -eq '.ps1' }

This will return files with the specified extension only.

Twitter This Tip! ReTweet this Tip!