fbpx Find Files That Match a Pattern in PowerShell | Windows PowerShell, Windows Server | HostingUltraso.com
24/7/365 Support

Find Files That Match a Pattern in PowerShell

Problem

You want to get a list of files that match a specific pattern.

Solution

Use the GetChildItem cmdlet for both simple and advanced wildcard support:

    • To find all items in the current directory that match a PowerShell wildcard, sup
    • ply that wildcard to the GetChildItem cmdlet: GetChildItem *.txt
    • To find all items in the current directory that match a providerspecific filter, sup
    • ply that filter to the –Filter parameter: GetChildItem –Filter *~2*
    • To find all items in the current directory that do not match a PowerShell wild
    • card, supply that wildcard to the –Exclude parameter: GetChildItem Exclude *.txt
  • To find all items in subdirectories that match a PowerShell wildcard, use

the –Include and –Recurse parameters: GetChildItem –Include *.txt –Recurse

• To find all items in subdirectories that match a providerspecific filter, use the

–Filter and –Recurse parameters: GetChildItem –Filter *.txt –Recurse

• To find all items in subdirectories that do not match a PowerShell wildcard, use the –Exclude and –Recurse parameters:

GetChildItem –Exclude *.txt –Recurse Use the WhereObject cmdlet for advanced regular expression support:

    • To find all items with a filename that matches a regular expression, use the
    • WhereObject cmdlet to compare the Name property to the regular expression: GetChildItem | WhereObject { $_.Name match '^KB[09]+\.log$' }
  • To find all items with a directory name that matches a regular expression, use the WhereObject cmdlet to compare the DirectoryName property to the regular expression:

GetChildItem –Recurse | WhereObject { $_.DirectoryName match 'Release' }

• To find all items with a directory name or filename that matches a regular expression, use the WhereObject cmdlet to compare the FullName property to the regular expression:

GetChildItem –Recurse | WhereObject { $_.FullName match 'temp' }

Discussion

The GetChildItem cmdlet supports wildcarding through three parameters:

Path The Path parameter is the first (and default) parameter. While you can enter simple paths such as ., C:\ or D:\Documents, you can also supply paths that include wildcards—such as *, *.txt, [az]???.log, or even C:\win*\*.N[af]?\ F*\v2*\csc.exe.

Include/Exclude The –Include and –Exclude parameters act as a filter on wildcarding that happens on the Path parameter. If you specify the –Recurse parameter, the –Include and –Exclude wildcards apply to all items returned.

The most common mistake with the –Include parameter comes when you use it against a path with no wildcards. For example, this doesn’t seem to produce the expected results:

GetChildItem $env:WINDIR Include *.log

That command produces no results, as you have not supplied an item wildcard to the path. Instead, the correct command is:

GetChildItem $env:WINDIR\* Include *.log

Filter The –Filter parameter lets you filter results based on the providerspecific filtering language of the provider from which you retrieve items. Since PowerShell’s wildcarding support closely mimics filesystem wildcards, and most people use the –Filter parameter only on the filesystem, this seems like a redundant (and equivalent) parameter. ASQL provider, however, would use SQL syntax in its –Filter parameter. Likewise, an Active Directory provider would use LDAP paths in its –Filter parameter.

Although it may not be obvious, the filesystem provider’s filtering language is not exactly the same as the PowerShell wildcard syntax. For example, the Filter parameter matches against the short filenames, too:

PS >GetChildItem | SelectObject Name

Name

A Long File Name With Spaces Also.txt A Long File Name With Spaces.txt

PS >GetChildItem *1* | SelectObject Name PS >GetChildItem Filter *1* | SelectObject Name

Name

A Long File Name With Spaces.txt

On the other hand, PowerShell’s wildcard syntax supports far more than the filesystem’s native filtering language. For more information about the PowerShell’s wildcard syntax, type GetHelp About_WildCard.

When you want to perform filtering even more advanced than what PowerShell’s wildcarding syntax offers, the WhereObject cmdlet provides infinite possibilities. For example, to exclude certain directories from a search:

GetChildItem Rec | WhereObject { $_.DirectoryName notmatch "Debug" }

or, to list all directories:

GetChildItem | WhereObject { $_.PsIsContainer }

Since the syntax of the WhereObject cmdlet can sometimes be burdensome for simple queries, the CompareProperty script provides an attractive alternative:

GetChildItem Rec | CompareProperty DirectoryName notmatch Debug For a filter that is difficult (or impossible) to specify programmatically, the SelectFilteredObject script lets you interactively filter the output.

Because of PowerShell’s pipeline model, an advanced file set generated by GetChildItem automatically turns into an advanced file set for other cmdlets to operate on:

PS >GetChildItem Rec | WhereObject { $_.Length gt 20mb } | >> SortObject Descending Length | SelectFilteredObject | >> RemoveItem WhatIf >> What if: Performing operation "Remove File" on Target "C:\temp\backup092300 .zip". What if: Performing operation "Remove File" on Target "C:\temp\sptricking_ iT2.zip". What if: Performing operation "Remove File" on Target "C:\temp\slime.mov". What if: Performing operation "Remove File" on Target "C:\temp\helloworld. mov".

For more information about the GetChildItem cmdlet, type GetHelp GetChildItem. For more information about the WhereObject cmdlet, type GetHelp WhereObject.

Help Category:

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 128 GB (SSD SATA) Configure Server
Intel Atom C2750 2.4 GHz 8c/8t 8 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Xeon E3-1230 v2 3.3 GHz 4c/8t 16 GB DDR3 1× 256 GB (SSD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 250 GB (SSD SATA) Configure Server

What Our Clients Say