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}'"
Answer from Mathias R. Jessen on Stack OverflowHow to expand variable in powershell? - Stack Overflow
powershell - %temp% etc not working - Stack Overflow
windows - Set nested expandable environment variable with PowerShell - Stack Overflow
Environment Variable inside a single '
How to set an environment variable using PowerShell?
To set an environment variable, you can use the following syntax.
$Env:VariableName = “Value”
Replace VariableName with the desired name of your variable and Value with the value you want to assign to it, as example below.
$Env:MY_VAR = “MyValue”
How to check if an environment variable exists in PowerShell?
To check if a specific environment variable exists, you can use the following command.
Test-Path Env:MY_VAR Replace MY_VAR with the name of the environment variable you want to check. If the variable exists, it will return True; if not, it will return False.
How do I list all variables in PowerShell?
To list all variables in your PowerShell session, you can use:
Get-ChildItem Env:
This command will show all currently defined variables in the session, including environment variables.
I was trying to add a environment variable to my user's Path using PowerShell, so I searched about and ended up with a code similar to this one:
[Environment]::SetEnvironmentVariable("TEST_VAR", "test-value", "User")
$currPath = [Environment]::GetEnvironmentVariable("Path", "User")
$newPath = "$currPath;%TEST_VAR%;"
[Environment]::SetEnvironmentVariable("Path", $newPath, "User")
The idea is to change TEST_VAR value in the future and by doing so updating the Path easily.
The problem I've found is that when setting Path with a non-expanded variable (in this case, %TEST_VAR%) it won't expand automatically. So, if I open the cmd and try to echo Path, I get something like this:
C:\Progr [...] omeOherStuff;%TEST_VAR%;
When it should be returning this:
C:\Progr [...] omeOherStuff;test-value;
Some places where I searched said that the answer to this problem is messing around with reg add, but I am not sure about it and even with some testing trying to set %TEST_VAR% as REG_EXPAND_SZ didn't work for me.
That is my first post here, also sorry if my english is bad. Thanks.
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, 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:
a = '
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.
If you are executing that line from PowerShell rather than from CMD, you can use the PowerShell environment variable syntax:
PS C:\> & "$env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe"
You can use [Environment]::ExpandEnvironmentVariables to expand environment variables within a string the old-fashioned way.
$s = '%systemroot%\temp'
[Environment]::ExpandEnvironmentVariables($s)
Hi Everyone,
I am not great with scripting, so apologies for not using the correct terminology and likely asking a very basic question. I am attempting to install an MSI package and my MSI package has a space in the name (I can't rename it, it has to be this name). I am invoking the start-process command with the below argument list.
-ArgumentList '/I "$env:temp\My MSI File.msi" /qn'
This fails and I believe it is due to the use of the single quote which I think it turning everything inside of it as a literal statement so it is not reading the environment variable etc. What do I need to do to get this to work?
Cheers!
***Edit, I figured it out. I needed to wrap what needed to be in quotes with an extra set of quotes and use quotes around the entire argument list like this:
-ArgumentList "/I ""$env:temp\My MSI File.msi"" /qn"