0

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.

2
  • Note that Select-String patterns are case-insensitive by default. Commented Jun 9, 2015 at 18:54
  • @mikez: Not only Select-String. All of PowerShell (except when calling .NET methods directly). Commented Jun 11, 2015 at 10:37

2 Answers 2

6

Very simple:

ls | where Name -match 'myregex'

There are other options, though:

(ls) -match 'myregex'

Or, depending on how complex your regex is, you could maybe also solve it with a simple wildcard match:

ls wild[ck]ard*.txt

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 to Get-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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that's educative. How does the second method work though, are the parantheses a shortcut for some command? (btw the [rR] was just to illustrate it's a regex, my actual filter is based on \d and anchors)
The second method, the parentheses have the common programming/math "do this first" meaning; without them it would try and give -match as a parameter to ls but with them, it lists the files then does -match against the array of results.
The second one exploits a feature of PowerShell where comparison operators return matching results when there's a collection on the left side. (ls) first evaluates completely, yielding an array of results, -match is then applied for every one of them, trying to match the string representation (the name) to the regex, and returning those that match.
1

While researching based on @Joey's answer, I stumbled upon another way to achieve the same (based on Select-String itself):

ls -Name | Select-String -Pattern 'my[Rr]egexp?'

The -Name argument seems to make ls return the result as a plain string rather than FileInfo object, so Select-String treats it as the string to be searched in rather than a list of files to be searched.

2 Comments

I'd really advise against this, though. PowerShell is designed to work with objects and while strings are objects, too, they're a poor substitute for richer ones, like FileInfo. Sure, you can go back to the Dark Age of Unix shell scripting, but you're missing out on a lot.
Haha yes, that was just my inner Unix geek finding comfort in a familiar plaintext pipe. Powershell's object functionalities do sound potentially much more powerful though, especially considering .NET is only a bracket away!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.