How to search for a string that starts with $ in a txt file?
For example:
Get-Content $file | Select-String -pattern 'VALUE="$USERPROFILE"'
Thanks
How to search for a string that starts with $ in a txt file?
For example:
Get-Content $file | Select-String -pattern 'VALUE="$USERPROFILE"'
Thanks
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
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