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 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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


Comments

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.