How-to: Environment Variables

Windows environment variables are visible as a PS drive called Env:

To list all the environment variables use: Get-Childitem env: (or just dir env:)

Each environment variable is an object that can be retrieved with Get-Childitem (or Get-Item) and enumerated with Get-Member

Display Environment Variables

Display the values of all environment variables:

Get-Childitem -path env:


gci env: | sort name

Display the value of the COMPUTERNAME environment variable:

Get-Childitem env:computername

This can be made easier if you first set-location (cd) to the Env: Drive

cd env:

Then to display a variable: get-childitem computername

Returning a string value with $env:
PowerShell can also address environment variables using the syntax $env:variable-name this had the advantage of returning a sytem.string rather than the DictionaryEntry object returned by get-item or gci.


$Computer = $env:computername
$AppDataFolder = "$env:appdata"
"The application data folder on computer $Computer is $AppDataFolder"

$full_path_to_FC = "$($env:systemroot)\System32\fc.exe"

Similarly to list the environment PATH:

$windows_path = $env:Path
$windows_path -split ';'

PSModulePath lists all the paths that PowerShell searches for modules and includes in its module auto-loading:

$powershell_path = $env:PSModulePath

Set Environment Variables

In an expression use this syntax: $env:VariableName = 'new-value'

For example, to append "finance" to the value of the TEAMS environment variable:

$env:TEAMS = $env:TEAMS + "finance"

You can also change environment variables with Set-Item, Remove-Item, and Copy-Item.

Set-Item -path env:TEAMS -value ($env:TEAMS + 'finance')

Note the use of parentheses.

When you change environment variables, the change affects only the current session, much like using the SET command in Windows. To make the changes permanent, you can change them in the registry or with a utility like SETX or with a .Net function. You must also have permission to change the values. Permanent changes will only affect future sessions.

Add C:\Batch to the current PATH (for the current session only)

$env:path +=';c:\Batch'

Delete Environment Variables

Setting a variable = an empty string will remove it completely:

$env:VariableName = ''

SET command (PowerShell function)

Here is a function to quickly list or set an environment variable, using syntax like the Windows CMD SET command (by Wes Haggard)

 # remove the SET alias (normally used for set-variable)
 if (test-path alias:set) { remove-item alias:set > $null }
 function set
  [string]$var = $args
  if ($var -eq "")
    {get-childitem env: | sort-object name}
    if ($var -match "^(\S*?)\s*=\s*(.*)$")
    {set-item -force -path "env:$($matches[1])" -value $matches[2];}
    {write-error "ERROR Usage: VAR=VALUE"}

PS C:\> Set testing=abc

PowerShell Providers

In addition to environment variables, PowerShell providers also provide access to other data and components in a similar way - resembling a file system drive. This allows you to access many different types of data in a consistent way.

Built-in Providers

Alias - Windows PowerShell aliases {Alias}
Certificate - X509 certificates for digital signatures {cert}
Environment - Windows environment variables {Env}
FileSystem - File system drives, directories and files {filesystem}
Function - Windows PowerShell functions {Function}
Registry - Windows registry {HKLM, HKCU}
Variable - Windows PowerShell variables {Variable}

In PowerShell a fully qualified path name takes this form:


Additional providers may also be created and installed - Get-PsProvider will list them all.
Enumerate the properties of a provider with Get-psdrive: Get-psdrive Function | format-list *

e.g. to list all certificates use: Get-Childitem cert:

cd cert:

“Most variables can show either an upward trend or a downward trend, depending on the base year chosen” ~ Thomas Sowell

Related PowerShell Cmdlets

How-to: Automatic variables - Variables created and maintained by PowerShell $_, $Args, $Error, $Home etc
How-to: Variables and Operators - Create variable, add to value, subtract, divide.
How-to: Preference variables - Preferences: Verbosity, Confirmations, ErrorAction, $Debug.
Get-PSDrive - Get drive information (DriveInfo)
Windows CMD equivalent: List of built-in Environment variables (includes CMD shell variables).
class: System.Collections.DictionaryEntry
[Environment]::SetEnvironmentVariable (.Net method)

Copyright © 1999-2024
Some rights reserved