Using Regular Expressions with Dir

by Nov 11, 2011

When you use Dir (alias: Get-ChildItem) to list folder contents, you can use simple wildcards but they do not give you much control. A much more powerful approach is to use regular expressions. Since Get-ChildItem does not support regular expressions, you can use Where-Object to filter the results returned by Dir. This line will get you any file with a number in its filename, ignoring numbers in the file extension:

dir $home -Recurse | Where-Object {$_.Name -match '\d.*?\.'}

Twitter This Tip!
ReTweet this Tip!