PowerShell Basics: Environment Variables -$Env: | Examples and Scripts

PowerShell Basics_ Environment Variables

PowerShell’s Env Drive

Think of Env: as a drive, it’s just like the C:, except Env: contains not folders but variables such as ‘Path’ or ‘Windir’.

We are going to use PowerShell to manipulate the System variables, this is an alternative to using the Windows GUI and navigating to the Control Panel, System and ‘Advanced system settings’.PowerShell Environmental Variables

Topics for Windows PowerShell’s Environmental Variables

Research with Get-PSDrive

It was thanks to Get-PSDrive that I discovered that PowerShell views Env as a drive.


Cert (Certificates)

See more on Get-PSDrive »

PowerShell Lists Environmental Variables

Once you know that Env is a drive, then you can list its variables and their values.  This is just like you would use PowerShell to list the folders and files in the C: drive.

# List PowerShell's Environmental Variables
Get-Childitem -Path Env:* | Sort-Object Name

Name (Key)           Value
——–                  —————————————
APPDATA C:\Users\Guy\AppData\Roaming
CommonProgramFiles C:\Program Files\Common Files
ComSpec C:\Windows\system32\cmd.exe
LOCALAPPDATA C:\Users\Guy\AppData\Local
OS Windows_NT
Path C:\Program Files\Common Files\  more…
PROCESSOR_IDENTIFIER Intel64 Family 6 Model 37 Stepping 5
ProgramData C:\ProgramData
PSModulePath C:\Users\Documents\WindowsPowerShell\Modules; more…
PUBLIC C:\Users\Public
SystemDrive C:
SystemRoot C:\Windows
TEMP C:\Users\Guy\AppData\Local\Temp
TMP C:\Users\Guy\AppData\Local\Temp
windir C:\Windows

Tip: When learning about PowerShell’s capabilities, I like to open the corresponding Windows GUI, in this case go to the Control Panel, System and Security, System, and then click on the link: Advanced system settings.

Guy Recommends:  Network Performance Monitor (FREE TRIAL)Review of Orion NPM v11.5

SolarWinds Network Performance Monitor (NPM) will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM on a 30-day free trial.

SolarWinds Network Performance Monitor Download 30-day FREE Trial

Example 1: $Env:WinDir

Windir is a very old Microsoft variable going back to Windows 95.  Most of the time windir will refer to C:\Windows, however, it is possible that the operating system is installed on another volume, in which case programs using the windir variable will find the operating system.

# Find the operating system files

Example 2: $Env:Path

Path is probably the most interesting variable from a scripting point-of-view, with a little work you can add values and thus expand the path.

Remember we are dealing with variables, hence $Env.

# List PowerShell's Paths

Note 1: You really do need that $dollar sign.  Plain Env:Path does not work here.

See also PowerShell’s Env:Path »

Example 3: $Env:Temp

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

See how to delete these temporary files »

Example 4: Creating and Email Address

Here is a basic example of taking information in an environmental variable and using it in a script.

$Email = $Env:Username + '@mycompany.com'

Example 5: Dealing with Non-alpha Numeric Characters

When you attempt to script variables containing dots or brackets, you get errors, the solution is to wrap the variable in ${braces}.

#Problem (Parenthesis brackets)
Solution {Enclose in braces}

Note 2: I use this technique for variables that contain dots.  For example, this is how I work with a variable called ${Env:Guy.Test}

Guy Recommends: SolarWinds Engineer’s Toolset (FREE TRIAL)Engineer's Toolset v10

This Engineer’s Toolset provides a comprehensive console of 50 utilities for troubleshooting computer problems.  Guy says it helps me monitor what’s occurring on the network, and each tool teaches me more about how the underlying system operates.

There are so many good gadgets; it’s like having free rein of a sweetshop.  Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools.  Try the SolarWinds Engineer’s Toolset on a 14-day free trial now!

SolarWinds Engineer's Toolset Download 14-day FREE Trial

Help About_Environment_Variables

For more ideas for scripting Environmental Variables, let us call for PowerShell’s internal help file.

Help About_Environment_Variables

Note 3:

See also PowerShell’s Get-PSDrive »

Summary of PowerShell’s Environment Variables

Once you realize PowerShell is dealing with a drive, then it’s second nature to display the environmental variables and their values with Get-ChildItem.

See more Windows PowerShell  examples of variables

Syntax   • PowerShell Variables   • Get-PSProvider   • PowerShell Dollar Variable

PowerShell Functions   • Get-PSDrive   • PowerShell New-PSDrive   • Remove-PSDrive

PowerShell Home   • Foreach loops   • PowerShell Foreach   • Foreach-Object cmdlet

Please email me if you have a better example script.  Also please report any factual mistakes, grammatical errors or broken links, I will be happy to  correct the fault.