The Multifunctioning DBA

Jan 20 2009   5:49PM GMT

Environmental Variables in Powershell

Colin Smith Colin Smith Profile: Colin Smith

I have a script that I run in production that requires the script to access the Environment Variables for the server the script is executing on. This is a very simple task in powershell and very very useful. In my script I am using it just to verify that the machine name that it is executing on is the same as the machine name that I want the script to execute on. This is because the script is run from a single location and runs on many servers. The script compares the name that I am inputting to the actual machine name before moving on. Here is how easy this is in Powershell:

$name = (get-content env:Computername)

$name will now hold the value of the environmental variable on that machine for ComputerName. Powershell makes this so easy by having env as a PSDrive. This means that you can connect to it like you would a drive letter and get information from it. try the following:

cd env:

ls

and here is a sample of the results.

PS C:\Documents and Settings\smithco> cd env:
PS Env:\> ls

Name                           Value
—-                           —–
Path                           D:\oracle\ora92\bin;C:\Program Files\Oracle\jre\1.3.1\bin;C:\Program Files\Oracle\jre\1.1.8\bin;D:…
TEMP                           C:\DOCUME~1\smithco\LOCALS~1\Temp
SESSIONNAME                    Console
PATHEXT                        .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.PSC1
APPDATA                        C:\Documents and Settings\smithco\Application Data
SYBASE                         D:\Apps\sybase
PROCESSOR_ARCHITECTURE         x86
SystemDrive                    C:
HOMESHARE                      \\pni-pcfs01\HOME
DEFLOGDIR                      C:\Documents and Settings\All Users\Application Data\McAfee\DesktopProtection
windir                         C:\WINDOWS
USERPROFILE                    C:\Documents and Settings\smithco
TMP                            C:\DOCUME~1\smithco\LOCALS~1\Temp
SYBASE_JRE                     D:\Apps\sybase\shared-1_0\jre1.2.2
USERDNSDOMAIN                  PNI.US.AD.GANNETT.COM
USERDOMAIN                     PNI
ProgramFiles                   C:\Program Files
FP_NO_HOST_CHECK               NO
HOMEPATH                       \
COMPUTERNAME                   PNI-BT15689
CLASSPATH                      .;D:\Apps\sybase\ASEP_Win32\3pclass.zip;D:\Apps\sybase\ASEP_Win32\monclass.zip;C:\Program Files\Ja…
USERNAME                       smithco
NUMBER_OF_PROCESSORS           2
PROCESSOR_IDENTIFIER           x86 Family 15 Model 4 Stepping 3, GenuineIntel
VSEDEFLOGDIR                   C:\Documents and Settings\All Users\Application Data\McAfee\DesktopProtection
INCLUDE                        D:\Apps\sybase\OCS-12_5\include;
ComSpec                        C:\WINDOWS\system32\cmd.exe
LOGONSERVER                    \\PNI-DVADDC02
SYBASE_OCS                     OCS-12_5
CommonProgramFiles             C:\Program Files\Common Files
SystemRoot                     C:\WINDOWS
PROCESSOR_LEVEL                15
PROCESSOR_REVISION             0403
QTJAVA                         C:\Program Files\Java\jre1.6.0_05\lib\ext\QTJava.zip
lib                            D:\Apps\sybase\OCS-12_5\lib
ALLUSERSPROFILE                C:\Documents and Settings\All Users
WF_RESOURCES                   D:\oracle\ora92\WF\RES\WFus.RES
OS                             Windows_NT
HOMEDRIVE                      H:

PS Env:\>

Pretty cool huh….

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: