PowerShell Environment Variables
Anonymous contributor
Published May 16, 2023
Environment variables store information related to the current environment like the Operating System and user sessions. These global variables are name-value pairs that can be accessed across commands and programs.
Listing Environment Variables
Running the Get-ChildItem cmdlet on the Env: drive lists all the environment variables defined in the current environment.
PS > Get-ChildItem Env:Name Value---- -----ALLUSERSPROFILE C:\ProgramDataAPPDATA C:\Users\Prince\AppData\RoamingCOMPUTERNAME DESKTOPComSpec C:\Windows\system32\cmd.exeHOMEDRIVE C:NUMBER_OF_PROCESSORS 24OS Windows_NTPATH C:\Program Files\Common Files\Oracle\Java\javapath;C:\Program ......
A particular environment variable can also be specified by appending its name to the Env: drive:
PS > Get-Childitem Env:HOMEDRIVEName Value---- -----HOMEDRIVE C:
Printing Environment Variables Values
An environment variable’s value can directly be printed by accessing the Value property:
PS > (Get-Childitem Env:HOMEDRIVE).ValueC:
Alternatively, a shorthand syntax can be used:
PS > $Env:HOMEDRIVEC:
Creating Environment Variables
The syntax for creating environment variables is:
$Env:EXAMPLE_ENV_VAR = "custom value"
Note: Per convention, environment variable names are capitalized.
All contributors
- Anonymous contributor
Learn PowerShell on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Jump into PowerShell through interactive lessons on variables, operators, control flow, objects, arrays, and functions.
- Beginner Friendly.6 hours