PS: Working with Environment Variables in PowerShell

Command to list all Environment Variables:

PS C:> Get-ChildItem Env: (You can also use get-item)

Name                           Value
—-                           —–
ALLUSERSPROFILE                C:ProgramData
APPDATA                        C:UsersgovardhanAppDataRoaming
CommonProgramFiles             C:Program FilesCommon Files
COMPUTERNAME                   TEST-PC1
ComSpec                        C:Windowssystem32cmd.exe
FP_NO_HOST_CHECK               NO
HOMEDRIVE                      C:
HOMEPATH                       Usersgovardhan
LOCALAPPDATA                   C:UsersgovardhanAppDataLocal
LOGONSERVER                    \TESTDOM-DC01
NUMBER_OF_PROCESSORS           2
OS                             Windows_NT
Path                           %SystemRoot%system32WindowsPowerShellv1.0…
PATHEXT                        .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;….
PGInstallDir                   C:Program FilesPowerGUI
PGSnippetPath                  C:Windowssystem32WindowsPowerShellv1.0Mo…
PROCESSOR_ARCHITECTURE         x86
PROCESSOR_IDENTIFIER           x86 Family 6 Model 15 Stepping 13, GenuineIntel
PROCESSOR_LEVEL                6
PROCESSOR_REVISION             0f0d
ProgramData                    C:ProgramData
ProgramFiles                   C:Program Files
PSModulePath                   C:UsersgovardhanDocumentsWindowsPowerShel…
PUBLIC                         C:UsersPublic
QTJAVA                         C:Program FilesJavajre6libextQTJava.zip
SESSIONNAME                    Console
SIKULI_HOME                    C:Sikuli
SystemDrive                    C:
SystemRoot                     C:Windows
TEMP                           C:UsersGOVARD~1AppDataLocalTemp
TMP                            C:UsersGOVARD~1AppDataLocalTemp
USERDNSDOMAIN                  TESTDOM.COM
USERDOMAIN                     TESTDOM
USERNAME                       Govardhan
USERPROFILE                    C:Usersgovardhan
windir                         C:Windows

PS C:>

 

List all environment variables matching a string:

PS C:> Get-ChildItem Env:pro* (You can also use get-item)

Name                           Value
—-                           —–
ProgramFiles                   C:Program Files
ProgramData                    C:ProgramData
PROCESSOR_IDENTIFIER           x86 Family 6 Model 15 Stepping 13, GenuineIntel
PROCESSOR_ARCHITECTURE         x86
PROCESSOR_LEVEL                6
PROCESSOR_REVISION             0f0d

PS C:>

 

Use Get-Item or Get-ChildItem for while working with Environment Variables?

You can use either Get-Item or Get-ChildItem to retrieve the environment variables via ENV provider (note NOT referred by $ENV) which gives you the same number of results as shown below:

PS C:> Get-Item Env: | wc -l
43
PS C:>
Get-ChildItem Env: | wc -l
43
PS C:>

When it matters using Get-Item or Get-ChildItem with environment variables?

It really matters when you try to refer the environment variables values via scalar notation (referred as $ENV:) as shown below:

PS C:> Get-Item $ENV:appdata

    Directory: C:UsersgovardhanAppData

Mode                LastWriteTime     Length Name
—-                ————-     —— —-
d—-         4/13/2011   3:56 PM            Roaming

PS C:> Get-ChildItem $ENV:appdata

    Directory: C:UsersgovardhanAppDataRoaming

Mode                LastWriteTime     Length Name
—-                ————-     —— —-
d—-         3/17/2011  10:48 AM            Adobe
d—-         3/16/2011  11:31 AM            WinRAR
d—-         3/16/2011  11:30 AM            WordWeb

PS C:>

Magic of using ENV and $ENV in PS programming: (Or Differences referring environment variables via ENV and $ENV)

PS C:> Get-ChildItem ENV:appdata

Name                           Value
—-                           —–
APPDATA                        C:UsersgovardhanAppDataRoaming

PS C:> Get-ChildItem $ENV:appdata

    Directory: C:UsersgovardhanAppDataRoaming

Mode                LastWriteTime     Length Name
—-                ————-     —— —-
d—-         3/17/2011  10:48 AM            Adobe
d—-         3/16/2011  11:31 AM            WinRAR
d—-         3/16/2011  11:30 AM            WordWeb

PS C:>

Leave a Reply

Your email address will not be published. Required fields are marked *