Share via

Selecting from text with select-string

YaroC 321 Reputation points
2022-05-10T10:03:11.043+00:00

I'm trying to find all occurrences of following pattern [int]$license in a script located on multiple machines. When I run $test | select-string -pattern '/[int/]' where $test holds the content of the file, this works as long as I won't add the dollar sign afterwards. I tried '/[int/]/$license' and '/[int/]`$license' but neither works which proves my regex is useless :) Can someone help with this?

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

Answer accepted by question author

Rich Matheisen 48,116 Reputation points
2022-05-10T15:02:24.48+00:00

In a regex the escape character is "\", not "/".

$test = 'write-host "bogus!" [int]$license Some Other stuff'
$test -match '\[int\]\$license'

Was this answer helpful?

0 comments No comments

2 additional answers

Sort by: Most helpful
  1. YaroC 321 Reputation points
    2022-05-16T09:48:31.957+00:00

    Indeed. Silly as it is it was the wrong character I was trying to use.

    Was this answer helpful?

    0 comments No comments

  2. Michael Taylor 61,221 Reputation points
    2022-05-10T15:07:07.573+00:00

    Try this $test | select-string -pattern '\[int\]\$license'

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.