Selecting from text with select-string

YaroC 316 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
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 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'
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Michael Taylor 60,326 Reputation points
    2022-05-10T15:07:07.573+00:00

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

    0 comments No comments

  2. YaroC 316 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.

    0 comments No comments

Your answer

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