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 value of the COMPUTERNAME environment variable:

Get-Childitem env:computername

Display the values of all environment variables:

Get-Childitem env:

gci env: | sort name

Enumerate all the environment variables:

get-childitem -path env:* | get-member

This is 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"


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

$powershell_path = $env:PSModulePath

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

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 and set environment variables, 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


Automatic variables - Variables created and maintained by PowerShell $_, $Args, $Error, $Home etc
Variables and Operators - Create, add, subtract, divide.
Get-PSDrive - Get drive information (DriveInfo)
class: System.Collections.DictionaryEntry
(PowerShell 3.0)
(PowerShell 3.0)

© Copyright SS64.com 1999-2017
Some rights reserved