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 Overflow
🌐
Microsoft
devblogs.microsoft.com › dev blogs › scripting blog [archived] › powertip: expand environmental variables with powershell
PowerTip: Expand Environmental Variables with PowerShell - Scripting Blog [archived]
August 5, 2014 - How can I use Windows PowerShell to find the value of a common environmental variable such as %username% or %computername%? Use the ExpandEnvornmentalVariables static method from the System.Environment .NET Framework class, for example: ...
🌐
The Lonely Administrator
jdhitsolutions.com › commandline › friday fun: expand environmental variables in powershell strings
Friday Fun: Expand Environmental Variables in PowerShell Strings • The Lonely Administrator
June 29, 2012 - You told PowerShell to add a literal string %TEST% to your path and it did. PowerShell can’t expand it like that. What you did was the same as this, which is much easier. [environment]::SetEnvironmentVariable(“Path”,”$env:path;%TEST%”,”user”) The CMD shell can’t expand that in the path.
Discussions

How to expand variable in powershell? - Stack Overflow
I didn't find expanding expressions in PowerShell, but here's what I found. # Let's set some varaible $ComputerName = 'some-value' # Let's store this variable name $name = 'ComputerName' # Get value by `Get-Item`. (Get-Item variable:$name).Value # throws an exception for undefined variables. # Get value by `Invoke-Expression` Invoke-Expression "`$variable:$name" Invoke-Expression "`$$name" ... (Get-Item env:$name).Value # throws an exception for undefined environments... More on stackoverflow.com
🌐 stackoverflow.com
powershell - %temp% etc not working - Stack Overflow
I have the below that is used as a batch file that launches powershell (too long to go over but it is used in another script). Anyway, I noticed the %systemroot%\temp and %systemroot% does not wor... More on stackoverflow.com
🌐 stackoverflow.com
windows - Set nested expandable environment variable with PowerShell - Stack Overflow
I have read many posts on this topic and tried many things but cannot seem to get this to work. I want to set an environment variable and then nest that variable in the Path environment variable. I More on stackoverflow.com
🌐 stackoverflow.com
Environment Variable inside a single '
There are a lot of different ways to do this, I like PowerShell's flexibility when dealing with strings. Single quotes do indeed turn everything literal. however, single quotes are literal inside of double quotes msiexec wont care if the path is encased in single or double quotes so you could use single quotes in your string -ArgumentList "/I '$env:temp\My MSI File.msi' /qn" You could also escape the double quote with a back tick -ArgumentList "/I `"$env:temp\My MSI File.msi`" /qn" Unrelated to your post but helpful when dealing with strings. If you are trying to access a property in an object that is a string it can be formatted like this "This is the users first name $($user.firstname)" More on reddit.com
🌐 r/PowerShell
6
14
November 10, 2021
People also ask

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”

🌐
netwrix.com
netwrix.com › home › resources › blog › powershell environment variables
PowerShell Environment Variables
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.

🌐
netwrix.com
netwrix.com › home › resources › blog › powershell environment variables
PowerShell Environment Variables
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.

🌐
netwrix.com
netwrix.com › home › resources › blog › powershell environment variables
PowerShell Environment Variables
🌐
Reddit
reddit.com › r/powershell › adding non-expanded environment variable to path, but it doesn't expand
r/PowerShell on Reddit: Adding non-expanded environment variable to Path, but it doesn't expand
April 15, 2024 -

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.

🌐
Netwrix
netwrix.com › home › resources › blog › powershell environment variables
PowerShell Environment Variables
August 25, 2025 - Appending values to existing environment variables in PowerShell can be useful when you want to expand the current configurations without overwriting their existing values. One common scenario for this is with the PATH environment variable, where you may want to add additional directories for ...
Find elsewhere
🌐
PowerShell Tips
powershelltips.com › home › system administration › powershell environment variables: get, set, and use them
PowerShell Environment Variables: Get, Set, and Use Them • PowerShell Tips
March 13, 2026 - Some paths stored in the registry or configuration files use the %VARIABLE% syntax (CMD-style). Use [System.Environment]::ExpandEnvironmentVariables() to expand them in PowerShell.
🌐
Computer Performance
computerperformance.co.uk › home › powershell
PowerShell Basics: $Env: - Environment Variables | Examples & Scripts
January 16, 2019 - Path is probably the most interesting ... and thus expand the path. Remember we are dealing with variables, hence $Env. ... Note 1: You really do need that $dollar sign. Plain Env:Path does not work here. ... This script will find temporary files; actually, it’s a precursor to using PowerShell to delete such temp files. # PowerShell Script to List Temp Files Get-Childitem $Env:Temp -Recurse ... Here is a basic example of taking information in an environmental variable and ...
🌐
Stack Overflow
stackoverflow.com › questions › 23813478 › set-nested-expandable-environment-variable-with-powershell
windows - Set nested expandable environment variable with PowerShell - Stack Overflow
#Environment Variable $HOME_VAR = "MAVEN_HOME" $HOME_PATH = "e:\Apps\maven\apache-maven-3.2.1" $APP_CMD = "mvn" $APP_ARGS = "--help" #String to be added to the Path $BIN_PATH = "%$HOME_VAR%\bin" #Registry location of Machine Environment variables $SYSVAR_REG_PATH = "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" #Get the correct hive $HKLM = [Microsoft.Win32.Registry]::LocalMachine #Get the registry key with true to indicate that it is for editing $sysvar_regkey = $HKLM.OpenSubKey($SYSVAR_REG_PATH, $TRUE) #Set the value in the registry $sysvar_regkey.SetValue($HOME_VAR, $HOME_PA
🌐
ITPro Today
itprotoday.com › home › powershell
Variable Expansion in PowerShell
November 29, 2024 - In some circumstances, PowerShell can even automatically translate the variable into its actual value as part of another string. For example, I could input the following: ... So, the first important point is, double-quoted strings expand environment variables, single-quoted strings do not.
🌐
Ais
ais.com › home › blog › expanding variable strings in powershell
Expanding Variable Strings in PowerShell - Applied Information Sciences
August 28, 2023 - As others suffering from this problem have suggested, wrapping this logic in some sort of version-aware PowerShell function is probably the best approach long term since you don’t have to worry about it. I’ve included an example below on a possible way to do this: if ($PSVersionTable.PSVersion.Major -lt 3) { $sourceString = $sourceString -replace '"', '`"' $sourceString = $sourceString -replace '''', '`''' } $ExecutionContext.InvokeCommand.ExpandString($sourceString) So, that’s it. Variable expansion within an input text file.
🌐
Reddit
reddit.com › r/powershell › environment variable inside a single '
r/PowerShell on Reddit: Environment Variable inside a single '
November 10, 2021 -

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"

🌐
GitHub
github.com › PowerShell › PowerShell › issues › 18126
Set and Get windows environment variables from powershell · Issue #18126 · PowerShell/PowerShell
September 19, 2022 - Summary of the new feature / enhancement I would like to add Set-WinEnvironmentVariable and Get-WinEnvironmentVariable cmdlets that work only in Windows environment so that I can manipulate environ...
Author   PowerShell
🌐
Petri
petri.com › home › powershell › powershell set environment variable – a step-by-step guide
PowerShell Set Environment Variable - A Step-By-Step Guide | Petri
May 5, 2025 - One of the easiest ways to add or append an environment variable using PowerShell is to use $Env to set an environment variable using the assignment operator (=) and to append or create a new environment variable using the (+=) operator.
🌐
GitHub
github.com › chocolatey › choco › issues › 1417
Refreshenv should expand variables recursively in case they're new (includes solution) · Issue #1417 · chocolatey/choco
October 5, 2017 - PS C:\Users\Graham> $VerbosePreference = "continue" PS C:\Users\Graham> Update-SessionEnvironment --verbose --debug VERBOSE: Refreshing environment variables from the registry. PS C:\Users\Graham> $env:thirdReference %secondReference% function ExpandEnvironmentVariablesRecursive($unexpanded) { $previous = '' $expanded = $unexpanded while($previous -ne $expanded) { $previous = $expanded $expanded = [System.Environment]::ExpandEnvironmentVariables($previous) } return $expanded } #Example usage [System.Environment]::SetEnvironmentVariable('somethingPreviouslyUndefined', 'successfully expanded', '
Author   chocolatey