Andyg_2020
Andyg_2020

Reputation: 151

Need a string with a $ in powershell inside ' '

I am trying to write a PS script to find a share named C$$ but PS is interpreting the $ sign and I want to pass it into the filter. Normally \ stops this but after googling I cant get a working solution.

$string = "Name='C\$\$'"
Write-Host $string
get-wmiobject -Class Win32_share -filter $string

The write host returns Name='C\$\$' I need it to return C$$

Upvotes: 11

Views: 16327

Answers (2)

Bacon Bits
Bacon Bits

Reputation: 32145

There's several ways to do this. Note in the first one that you only need to escape the first $.

$string = "Name='C`$$'"
$string = "Name='C$$'"
$string = 'Name=''C$$'''
$string = "Name='C{0}'" -f '$$'
$string = @'
Name='C$$'
'@
$string = @"
Name='C`$$'
"@

Upvotes: 4

Bryce McDonald
Bryce McDonald

Reputation: 1870

You can use the backtick to escape characters in a string.

$string = "Name='C`$`$'"

That should return Name='C$$'

Upvotes: 16

Related Questions