Published May 16, 2023
Contribute to Docs
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
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
PS > Get-Childitem Env:HOMEDRIVEName Value---- -----HOMEDRIVE C:
Printing Environment Variables Values
An environment variable’s value can directly be printed by accessing the
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.
- Anonymous contributorAnonymous contributor
- Anonymous contributor
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PowerShell on Codecademy
Computer ScienceLooking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
Includes 6 Courses
With Professional Certification