Searching Files with Regular Expressions

by Aug 18, 2014

All PowerShell Versions

Get-ChildItem does not support advanced file filtering. While you can use simple wildcards, you cannot use regular expressions.

To work around this, add a cmdlet filter and use the operator -match.

This example will find all files within your Windows directory structure that have file names with at least 2-digit numbers in them, and a maximum file name length of 8 characters:

Get-ChildItem -Path $env:windir -Recurse -ErrorAction SilentlyContinue |
  Where-Object { $_.BaseName -match '\d{2}' -and $_.Name.Length -le 8 } 

Note the use of the property "BaseName". It returns the filename without an extension. This way, numbers in file extensions won't count.

Twitter This Tip! ReTweet this Tip!