Bookmark and Share
 

Microsoft Scripting Guy Ed Wilson here. I spent the day at SQL Saturday in Charlotte, NC. I could not resist the opportunity to network with that many rabid Windows PowerShell addicts. I mean, anyone who gives up their Saturday to come to a Microsoft office and sit in training sessions all day is dedicated. Definitely my type of geek. It was a wonderful day, and I was able to meet some tweeps (Twitter followers) in real life.

During one of the breaks, I was talking to a scripter from Charleston, South Carolina. Charleston is a beautiful town with lots of old houses, as seen in this picture I took a few years ago when I was in town to teach a VBScript class.

Photograph taken in Charleston, South Carolina


He was asking about accessing environmental variables from within Windows PowerShell. I told him there were several ways, but he did not seem to believe me, and thought it was a rather difficult task.

One way to access the environmental variables is to use the Windows PowerShell environmental drive. To do this, you use the $env: variable, which represents the environmental drive. You then access the specific variable by using its name. For example, the value of the os environmental variable is seen here:

PS C:\> $env:os

Windows_NT

PS C:\>

Another way to access environmental variables is by using WMI and querying the Win32_Environment WMI class. This is shown here:

PS C:\> get-wmiobject win32_environment | where-object { $_.name -eq 'os' }

VariableValue                           Name
-------------                           ----
Windows_NT                              OS


PS C:\>

You can use the wshEnvironment object that is obtained from the environment property of the WshShell object:

PS C:\> $wshShell = New-Object -ComObject wscript.shell
PS C:\> $wshShell.Environment().item("os")
Windows_NT
PS C:\>

In addition to working directly with the wshEnvironment object, the WshShell object also contains a method called ExpandEnvironmentStrings that can be used to translate the value of an environmental variable. This is seen here:

PS C:\> $wshShell = New-Object -ComObject wscript.shell
PS C:\> $wshShell.ExpandEnvironmentStrings("%os%")
Windows_NT
PS C:\>

One of my favorite tricks is to run the command interpreter inside Windows PowerShell. By running the command interpreter, you have access to the set command. You can use set to query the current value of an environmental variable as well as change the value. This is seen here (remember, that you will need to type exit to leave the cmd prompt and return to Windows PowerShell):

PS C:\> cmd
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\>set os
OS=Windows_NT

C:\>exit
PS C:\>

One last way to obtain the value of an environmental variable is to use the System.Environment .NET Framework class. The System.Environment .NET Framework class has the GetEnvironmentVariable static method that accepts an environmental variable as an argument. This is seen here:

PS C:\> [environment]::GetEnvironmentVariable("os")
Windows_NT
PS C:\>

Well, there are a half dozen different ways of accessing the same environmental variable. I am sure that there are more, but right now, it is getting kind of late (it has been a rather long day), and I cannot think of any more ways at this tired moment. If you think of any more ways to access environmental variables, send e-mail to us at scripter@microsoft.com and I will collect the new ways, and talk about them.

If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at scripter@microsoft.com or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys