PowerShell: List Files by File Name Pattern (grep file names)
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.
- 🛑 WARNING: when you use
-include
or-exclude
, you must list by dir content, e.g.dir *
or with-recurse
- 💡 TIP:
-include
or-exclude
is MUCH slower than using-filter
-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"}