Table of Contents
💡TL;DR
To print environment variables in PowerShell, useGet-ChildItem
cmdlet as below:
123 Get-ChildItem env:
Environment variables store values used by the operating system and installed programs. For instance, the environment variable PATH
contains a list of directories to search for executable files.
Some other environment variables in the Windows operating system are OS
, APPDATA
, COMPUTERNAME
, USERNAME
, HOMEDRIVE
, etc. PowerShell allows you to access and manage these variables of the system.
You can view, add, change, and delete environment variables from the PowerShell console. In this tutorial, you will learn different methods to print environment variables using PowerShell.
Print Environment Variables in PowerShell using Get-ChildItem
The Get-ChildItem
cmdlet displays the items and child items found in the specific location. For example, you can get the content of directories such as filenames and sub-directories.
The environment variables are stored in the Env:
drive. You can use the Get-ChildItem
to get all environment variables available in Env:
.
To print all environment variables in PowerShell, you can run:
1 2 3 |
Get-ChildItem env: |
Output:
The Get-ChildItem
has three aliases: dir
, gci
, and ls
.
1 2 3 |
Get-Alias -Definition Get-ChildItem |
Output:
1 2 3 4 5 6 7 |
CommandType Name Version Source ----------- ---- ------- ------ Alias dir -> Get-ChildItem Alias gci -> Get-ChildItem Alias ls -> Get-ChildItem |
You can use any of these aliases and display the environment variables in PowerShell.
For example,
1 2 3 |
dir env: |
OR
1 2 3 |
gci env: |
OR
1 2 3 |
ls env: |
Print Environment Variables in PowerShell using Get-Item
The Get-Item
cmdlet can also be used to print the system’s environment variables in PowerShell.
Run the Get-Item
command followed by the env:
drive.
1 2 3 |
Get-Item env: |
Output:
The Get-Item
has an alias gi
. So, you can also use:
1 2 3 |
gi env: |
Print Values of Environment Variables in PowerShell
If you want to get the values of a particular environment variable, you can use the following syntax.
1 2 3 |
$env:variable-name |
For example, to print the value of the environment variable OS
, you can run:
1 2 3 |
$env:os |
It returns a string value of the given environment variable.
Output:
1 2 3 |
Windows_NT |
Note: The environment variable names in Windows are not case-sensitive. But, they are case-sensitive in Linux and macOS.
Print Values of Environment Variables using Get-ChildItem
Another method to display the value of an environment variable is to use Get-ChildItem
or its aliases.
To view the value of PATHEXT
, execute this command.
1 2 3 |
Get-ChildItem env:pathext |
1 2 3 4 5 |
Name Value ---- ----- PATHEXT .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL |
In this article, we have discussed several methods to print environment variables and their values in PowerShell. The commands like Get-ChildItem
, dir
, ls
, and gci
come in handy when you need to know the system’s environment variables.
We hope this tutorial has helped you to get environment variables using PowerShell.