I need to do something similar to Unix's ls | grep 'my[rR]egexp?'
in Powershell. The similar expression ls | Select-String -Pattern 'my[rR]egexp?'
seems to go through contents of the listed files, rather than simply filtering the filenames themselves.
The Select-String documentation hasn't been of much help either.
Very simple:
There are other options, though:
Or, depending on how complex your regex is, you could maybe also solve it with a simple wildcard match:
which is faster than above options. And if you can get it into a wildcard match without character classes you can also just use the
-Filter
parameter toGet-ChildItem
(ls
), which performs filtering on the file system level and thus is even faster. Note also that PowerShell is case-insensitive by default, so a character class like[rR]
is unnecessary.