How to get environment variable value using PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

Environment variables are an essential part of the Operating System. They store various information like the path of the system files and folders, the number of processors system running, current user details, and more. Processes and programs utilize these environment variables to retrieve the data for their execution.

Environment variables in PowerShell are stored as PS drive (Env: ). To retrieve all the environment variables stored in the OS you can use the below command.

Get-ChildItem -Path Env:
Name                        Value
----                        -----
ALLUSERSPROFILE             C:\ProgramData
APPDATA                     C:\Users\delta\AppData\Roaming
CommonProgramFiles          C:\Program Files\Common Files
CommonProgramFiles(x86)     C:\Program Files (x86)\Common Files
CommonProgramW6432          C:\Program Files\Common Files
COMPUTERNAME                TEST1-WIN2K12
ComSpec                     C:\Windows\system32\cmd.exe
FP_NO_HOST_CHECK            NO
HOMEDRIVE                   C:
HOMEPATH                    \Users\delta
LOCALAPPDATA                C:\Users\delta\AppData\Local
LOGONSERVER                 \\ADDC
NUMBER_OF_PROCESSORS        1
OS                          Windows_NT
Path                        C:\Windows\system32;C:\Windows;C:\Windows\System32
\Wbem
PATHEXT                    .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.
MSC;.
PROCESSOR_ARCHITECTURE     AMD64
PROCESSOR_IDENTIFIER Intel64 Family 6 Model 126 Stepping 5, GenuineInte

You can also use dir env: command to retrieve all environment variables and values.

To retrieve a specific environment variable, provide variable name after env:

PS C:\Windows\system32> Get-ChildItem -Path Env:\SystemDrive
Name          Value
----          -----
SystemDrive   C:

If there are multiple values displayed for the environment variable then you can separate values using Split operation. For example,

$env:PSModulePath -split ';'

Output:

C:\Users\delta\Documents\WindowsPowerShell\Modules
C:\Program Files\WindowsPowerShell\Modules
C:\Windows\system32\WindowsPowerShell\v1.0\Modules

You can also use the .NET method of class [System.Environment] to retrieve the specific or all environment variables. To retrieve all environment variables use GetEnvironmentVariables() class.

For example,

PS C:\Windows\system32> [System.Environment]::GetEnvironmentVariables()
Name Value
---- -----
COMPUTERNAME TEST1-WIN2K12
USERPROFILE                    C:\Users\delta
HOMEPATH                       \Users\delta
LOCALAPPDATA                   C:\Users\delta\AppData\Local
PSModulePath                   C:\Users\delta\Documents\WindowsPowerShell\Modules
PROCESSOR_ARCHITECTURE         AMD64
Path                           C:\Windows\system32;C:\Windows;C:\Windows\System32
\Wbem
CommonProgramFiles(x86)        C:\Program Files (x86)\Common Files
ProgramFiles(x86)              C:\Program Files (x86)
PROCESSOR_LEVEL                6
LOGONSERVER                   \\ADDC
PATHEXT                       .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.
MSC;.CPL

To get the specific environment variable using .Net method use GetEnvironmentVariable() method.

PS C:\Windows\system32> [System.Environment]::GetEnvironmentVariable('appdata')
C:\Users\delta\AppData\Roaming

See the link below for all the environment system class supported properties and methods.

https://docs.microsoft.com/en-us/dotnet/api/system.environment?view=netcore-3.1

If you check the above link, the GetEnvironmentVariable method supports multiple arguments.

GetEnvironmentVariable(String, EnvironmentVariableTarget)

We can pass the environment variable as a string and Machine as the EnvironmentVariableTarget in the example below.

[System.Environment]::GetEnvironmentVariable('PSModulePath', 'Machine')

Output:

C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\
v1.0\Modules
raja
Published on 05-Oct-2020 06:59:01
Advertisements