Categories
PowerShell Quick Tips Regex

Using Case-Sensitive Regular expressions in PowerShell – Tips

Say you want to find functions that uses parameters named like:
-AsString, -AsHashTable, or -AsByteStream

Using Find-Member makes it easy.

> Find-Member -Name 'As*'

That’s close, but I want it to start with a capital letter, to skip matches like:
Assembly or Asin

Converting Like to a Regex

The -like pattern As* is the same as the regex ^As
To start with a capital letter you could use: ^As[A-Z]

Disabling Case-Sensitive matches

Because PowerShell defaults to case-insensitive matches
You need to remove the i insensitive flag using the syntax (?-i)

Solution

> Find-Member -Name '(?-i)^As[A-Z]' -RegularExpression

The final pattern is (?-i)^As[A-Z]
Making it case-sensitive narrowed down the matches from 113 to 43 !

See More