PowerShell: List Files by File Name Pattern (grep file names)

By Xah Lee. Date: . Last updated: .

Filter File Name by Wildcard (-filter)

-filter parameter takes a Wildcards pattern to show only those files. Useful if you want to list files of a specific filename extension.

You cannot use the filter parameter to filter multiple filename extensions.

# list jpg files
dir -recurse -file -filter *.jpg
# list emacs backup files (file name ending in ~)
dir -recurse -file -filter *~

Filter File Name by Multiple File Extensions

You can use -include or -exclude with Wildcards to filter out files by multiple file name extensions. e.g. -include *.jpg,*.png

the Exclude parameter is applied after the Include parameter.

-include

dir -recurse -file -include *.jpg,*.jpeg
# or
dir -recurse -file -include "*.jpg","*.jpeg"

-exclude

dir -recurse -file -exclude *.jpg,*.jpeg

Filter File Name by Regular Expression

The where-object cmdlet is useful to filter files by a boolean test of Regular Expression pattern match.

# list jpg files
dir -recurse -file | where { $_.name -match "\.jpg|\.jpeg|\.jfif"}

PowerShell, List Files