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

By Xah Lee. Date: . Last updated: .

Filter File Names 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 for multiple 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 (-include / -exclude)

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

-include

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

-exclude

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

Filter File Names by Regular Expression

The where-object cmdlet is useful to filter files by a boolean test of Regular Expression pattern match. [see Pipeline Commands] This is the most powerful, but not intuitive.

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

Note: -iMatch ignore letter case. [see String Operators]

PowerShell

Practical How-To

list files

list dirs

on dir

on file

Windows config

misc


PowerShell in Depth

Programing PowerShell

Value Types

String

Variable

Boolean

Conditional

Data Structure

Loop and Iteration

Input/Output

Function

Profile and Script

Script Examples