Home > Powershell > Environmental Variables the Powershell Way

Environmental Variables the Powershell Way

During a recent class I was showing the students how to join a server to the domain using the following netdom command line utility:



During the explanation of the parameters involved I realized that none of the students had ever used or even heard of environmental variables, which in the above example is %computername%.   Environmental variables, like all variables in scripting, are used as placeholders to store data.  Windows uses them to store information such as the location of the Windows folder (%SystemRoot%) or the currently logged on user’s name (%Username%). 

All the current Windows Environmental variables can be viewed using the Set command via the command line as seen below:

Although this would be sufficient the question then arose how can I do this in Powershell.  It just so happens that the Set command is not used in the same context in Powershell.  Typing Set in Powershell will reveal the following:

Not sure exactly how to use the above results so I used the Powershell help command to assist:

It appears that the Set command is used in conjuction with Variable.   Although Set didn’t work as I intended I knew there must be a way to view the Windows Environmental variables somehow.  Well it just so happens that I was reading an old issue of Windows IT Pro magazine (July 2008) which had an article on using Poweshell providers and drives.  Powershell supports an number of drives that provide access to data stores, such as the C:, D:, and Registry.  The full list is below:

One of the providers is Environment and is accessed using Env.  First we need to set our location to Env:

 Once there all we need to do is a directory listing using any of the following command: Dir, Ls or Get-Childitem (GCI)

And there you have it.  But wait there’s more.  Powershell also has it’s own variables that can be access using the Variable PSProvider.  We just need to change directories using the set command and then use ls to display the contents:

It’s amazing how such a simple request could reveal so much information.  I usually tell the students that you can create a blog article from any question or curriosity you may have and this was no exception.

Advertisements
Categories: Powershell
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: