Get-WmiObject -Class win32_logicaldisk -Filter 'DeviceID="C:"'
does what I want,
$var="C:"
Get-WmiObject -Class win32_logicaldisk -Filter 'DeviceID="{$var}"'
does nothing. I tried to change the quotes, to concatenate strings, and other 1000 things, but they didn't work. Why the above example doesn't work, and what would work?
When you start a string literal with '
(single-quotes), you're creating a verbatim string - that is, every character inside the string is interpreted literally and variable references and expressions won't expand!
Use "
if you want the variable to be expanded:
$var = 'C:'
Get-WmiObject -Class win32_logicaldisk -Filter "DeviceID='$var'"
If your variable name has weird characters, or is followed by a word character, you can qualify the variable name with curly brackets {}
immediately after the $
:
$var = 'C:'
Get-WmiObject -Class win32_logicaldisk -Filter "DeviceID='${var}'"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With