Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

How to Get Environment Variable Values Using PowerShell

July 22, 2024
in A, Blog, Powershell
0
ADVERTISEMENT

Table of Contents

Get Environment Variable Values Using PowerShell

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 PSDrive (Env:). First, let’s show the list of PSDrive on this computer using Get-PSDrive cmdlet.

Get-PSDrive
Name           Used (GB)     Free (GB) Provider      Root                                               CurrentLocation
----           ---------     --------- --------      ----                                               ---------------
Alias                                  Alias
C                  20.92        105.53 FileSystem    C:\
Cert                                   Certificate   \
D                   0.10        384.89 FileSystem    D:\
Env                                    Environment
Function                               Function
HKCU                                   Registry      HKEY_CURRENT_USER
HKLM                                   Registry      HKEY_LOCAL_MACHINE
Variable                               Variable
WSMan                                  WSMan

To retrieve all the environment variables stored in the OS you can use the below command:

Get-ChildItem -Path Env:

The output looks something like this, where the variable’s name is listed first, and then the value:

Name                           Value
----                           -----
ALLUSERSPROFILE                C:\ProgramData
APPDATA                        C:\Users\admin\AppData\Roaming
CLIENTNAME                     win11
CommonProgramFiles             C:\Program Files\Common Files
CommonProgramFiles(x86)        C:\Program Files (x86)\Common Files
CommonProgramW6432             C:\Program Files\Common Files
COMPUTERNAME                   ADMIN-NHK7SI58S
ComSpec                        C:\Windows\system32\cmd.exe
DriverData                     C:\Windows\System32\Drivers\DriverData
HOMEDRIVE                      C:
HOMEPATH                       \Users\admin
LOCALAPPDATA                   C:\Users\admin\AppData\Local
LOGONSERVER                    \\ADMIN-NHK7SI58S
NUMBER_OF_PROCESSORS           8
OneDrive                       C:\Users\admin\OneDrive
OS                             Windows_NT
Path                           C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPo...
PATHEXT                        .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL
POSH_INSTALLER                 winget
POSH_THEMES_PATH               C:\Users\admin\AppData\Local\Programs\oh-my-posh\themes
PROCESSOR_ARCHITECTURE         AMD64
PROCESSOR_IDENTIFIER           AMD64 Family 25 Model 1 Stepping 1, AuthenticAMD
PROCESSOR_LEVEL                25
PROCESSOR_REVISION             0101
ProgramData                    C:\ProgramData
ProgramFiles                   C:\Program Files
ProgramFiles(x86)              C:\Program Files (x86)
ProgramW6432                   C:\Program Files
PSModulePath                   C:\Users\admin\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell...
PUBLIC                         C:\Users\Public
SESSIONNAME                    31C5CE94259D4006A9E4#0
SystemDrive                    C:
SystemRoot                     C:\Windows
TEMP                           C:\Users\admin\AppData\Local\Temp
TMP                            C:\Users\admin\AppData\Local\Temp
USERDOMAIN                     ADMIN-NHK7SI58S
USERDOMAIN_ROAMINGPROFILE      ADMIN-NHK7SI58S
USERNAME                       admin
USERPROFILE                    C:\Users\admin
windir                         C:\Windows
WSLENV                         WT_SESSION:WT_PROFILE_ID:
WT_PROFILE_ID                  {61c54bbd-c2c6-5271-96e7-009a87ff44bf}
WT_SESSION                     2e5ffde5-eda5-4206-a2db-631c1273db04

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

dir Env:

Environment variable scopes

Before you are moving forward, you should know about the Scope. On Windows, environment variables can be defined in three scopes:

  • Process (or session) scope: The Process scope contains the environment variables available in the current process, or PowerShell session. This list of variables is inherited from the parent process and is constructed from the variables in the Machine and User scopes. Closing the PowerShell window will revert the environment variables to its pre-determined state
  • User scope: User environment variables are specific only to the currently logged-in user.
  • System (or Machine) scope: System environment variables are globally accessed by all users.
PZYOB7XxNDHkveS0DMtGuHwSdFdwfcLJigCrBJY3stuL5qLlJTar4YDzJBZv

Retrieve a specific environment variable

To retrieve a specific environment variable, provide variable name after Env:. For example, we will get the values of the Path environment varialbe:

(Get-ChildItem -Path Env:\Path).Value
# Output
C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Users\mpnadmin\AppData\Local\Microsoft\WindowsApps;;C:\Users\mpnadmin\AppData\Local\Programs\oh-my-posh\bin;C:\Users\mpnadmin\AppData\Local\Programs\Microsoft VS Code\bin;C:\Scripts

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

(Get-ChildItem -Path Env:\Path).Value -split ";"
C:\Windows\system32
C:\Windows
C:\Windows\System32\Wbem
C:\Windows\System32\WindowsPowerShell\v1.0\
C:\Windows\System32\OpenSSH\
C:\Users\mpnadmin\AppData\Local\Microsoft\WindowsApps
C:\Users\mpnadmin\AppData\Local\Programs\oh-my-posh\bin

Additionally, the Path environment variable values can be retieved by the simply below command:

$env:Path -split ';'

The output of the above command will include the values in the both user and system scope of the Path environment variables.

# Output
C:\Windows\system32
C:\Windows
C:\Windows\System32\Wbem
C:\Windows\System32\WindowsPowerShell\v1.0\
C:\Windows\System32\OpenSSH\
C:\Users\mpnadmin\AppData\Local\Microsoft\WindowsApps
C:\Users\mpnadmin\AppData\Local\Programs\oh-my-posh\bin
C:\Users\mpnadmin\AppData\Local\Programs\Microsoft VS Code\bin

Using the [System.Environment] class

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.

[System.Environment]::GetEnvironmentVariables()
Name                           Value
----                           -----
SystemDrive                    C:
ProgramFiles(x86)              C:\Program Files (x86)
Path                           C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPo...
ProgramW6432                   C:\Program Files
PROCESSOR_IDENTIFIER           AMD64 Family 25 Model 1 Stepping 1, AuthenticAMD
TMP                            C:\Users\mpnadmin\AppData\Local\Temp
PROCESSOR_ARCHITECTURE         AMD64
PATHEXT                        .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL
WSLENV                         WT_SESSION:WT_PROFILE_ID:
PROCESSOR_REVISION             0101
...
# The output is truncated

To get the specific environment variable using .NET method you can use GetEnvironmentVariable() method. Go to this link for all the environment system class supported properties and methods.

[System.Environment]::GetEnvironmentVariable('Path') -split ";"
C:\Windows\system32
C:\Windows
C:\Windows\System32\Wbem
C:\Windows\System32\WindowsPowerShell\v1.0\
C:\Windows\System32\OpenSSH\
C:\Users\mpnadmin\AppData\Local\Microsoft\WindowsApps
C:\Users\mpnadmin\AppData\Local\Programs\oh-my-posh\bin
C:\Users\mpnadmin\AppData\Local\Programs\Microsoft VS Code\bin

This method supports retrieving the values in user scope and system scope separately. We can pass the environment variable as a string and Machine or User as the EnvironmentVariableTarget in the example below.

Get the value in the User scope:

[System.Environment]::GetEnvironmentVariable('PATH','User') -split ";"
C:\Users\mpnadmin\AppData\Local\Microsoft\WindowsApps
C:\Users\mpnadmin\AppData\Local\Programs\oh-my-posh\bin
C:\Users\mpnadmin\AppData\Local\Programs\Microsoft VS Code\bin

Get the value in the system scope:

[System.Environment]::GetEnvironmentVariable('PATH','Machine') -split ";"
C:\Windows\system32
C:\Windows
C:\Windows\System32\Wbem
C:\Windows\System32\WindowsPowerShell\v1.0\
C:\Windows\System32\OpenSSH\

Using Windows Registry

And the last way, the Path environment variable stores its value in registry. So, we can get it from PowerShell as well. On Windows, environment variables can be defined in:

User Variables: HKEY_CURRENT_USER\Environment

System Variables: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

u3m0ZJgoiEnOOZs2y1TDHwJhqq1lGGGZtUhldVDd9t6qyMMPEt6vfgMNwmE3

PowerShell command to get the value of the Path environment variables in the user scope:

(Get-ItemProperty HKCU:\Environment).Path -split ";"
aIvs70q62Lki64Ya2f0sbaxphcLB0UuStaer5LbQCzdzryBn9PsuXLMnPozV

PowerShell command to get the value of the Path environment variables in the system scope. In the below command gp is the alias of the Get-ItemProperty cmdlet in PowerShell.

(gp "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment").Path -split ";"
r8Q3IgcwXz3ASCTKhXDhOoKgWwFrDk6TSsAubHpuyTBFIVY3uQ40PbBbpUWJ
ADVERTISEMENT

Not a reader? Watch this related video tutorial:

5/5 - (1 vote)
Previous Post

How to Print PATH Environment Variables to The Console in PowerShell

Next Post

VMware Workstation Pro 17 Windows Where to Download?

Related Posts

Images Hidden Due To Mature Content Settings In CivitAI

August 31, 2024

Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

August 20, 2024

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024

How to Remove The Test Mode Watermark Without Disabling Test Mode

July 28, 2024

Leave a Reply Cancel reply

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

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory