1

How to search for a string that starts with $ in a txt file?

For example:

Get-Content $file | Select-String -pattern 'VALUE="$USERPROFILE"'

Thanks

2 Answers2

2

Either use the [regex]::Escape() method to correctly escape verbatim strings for use in regex patterns:

$pattern = 'VALUE="{0}"' -f [regex]::Escape('$USERPROFILE')
Get-Content $file | Select-String -Pattern $pattern

or use the -SimpleMatch switch to indicate you don't wanna use regex at all:

Get-Content $file | Select-String -pattern 'VALUE="$USERPROFILE"' -SimpleMatch
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
0

If you only want to find strings that start with '$', you can use an approach similar to the one below.

param(
    [string] $file = "$PSScriptRoot\test.txt",
    [string] $pattern = "$*"
)

$stringsBeginningWithPattern = (Get-Content $file).Split() | Where-Object {
    $_ -like $pattern
}

$stringsBeginningWithPattern


derekbaker783
  • 8,109
  • 4
  • 36
  • 50