How to expand variable in powershell?

后端 未结 3 1332
孤街浪徒
孤街浪徒 2021-01-06 02:35
Get-WmiObject -Class win32_logicaldisk -Filter \'DeviceID=\"C:\"\'

does what I want,

$var=\"C:\"
Get-WmiObject -Class win32_logical         


        
3条回答
  •  清酒与你
    2021-01-06 03:07

    If, for instance, you're getting data from a file with select-string, the return is a single quote string. If that string contains variables they won't expand. Invoke-Expression can be used if the variable is clean - not mixed in with other text:

    $abc = 123
    $a = '$abc'
    iex $a -> 123
    

    If the variable is part of a path name, this doesn't work. Use $ExecutionContext.InvokeCommand.ExpandString($var)

    $path = '$Home/HiMum'
    $ExecutionContext.InvokeCommand.ExpandString($path)
    -> /home/JoeBlogs/HiMum
    

    You lucky sods on Windows might be able to use Convert-String to change singles to doubles.

提交回复
热议问题