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 values

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

Return a string value.

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.

$env:computername

Store the 'temp' environment variable in a powershell (local) variable:

$result = $env:temp

Set values

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"

To change environment variables use Set-Item, Remove-Item, and Copy-Item.

For example:

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

Note the use of parentheses above.

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 have to change them in the registry or with a utility like SETX. You must also have permission to change the values.

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}
   else
    {
    if ($var -match "^(\S*?)\s*=\s*(.*)$")
    {set-item -force -path "env:$($matches[1])" -value $matches[2];}
    else
    {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:

filesystem::c:\windows\system32\shell.dll

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:
gci

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

Related:

List of Standard (built-in) Environment Variables.
Variables and Operators - Create, add, subtract, divide.
Get-PSDrive - Get drive information (DriveInfo)
class: System.Collections.DictionaryEntry
[system.environment]::Is64BitOperatingSystem
(PowerShell 3.0)
[system.environment]::Is64BitProcess
(PowerShell 3.0)



Back to the Top

© Copyright SS64.com 1999-2013
Some rights reserved