What is an environment variable in Windows? How to print environment variables in PowerShell?
Environment variables stores information about operating system environment information and programs. This information details include the operating system path, location of the windows installation directory, number of processes used by the operating system. PowerShell can access and manage or change environment variables.
Using the below commands in PowerShell, show environment variables to console
- dir env:
- gci env:
- ls env:
- Get-ChildItem env:
In the below image,
dir:env in PowerShell, print environment variables to console as environment variable name and environment variable value as the path of an environment variable.
In this tutorial, I will explain how to print environment variables or echo environment variables using PowerShell.
ls are the PowerShell
Get-ChildItem aliases to show environment variables.
PowerShell Print Environment Variables
Let’s check each one of the PowerShell commands to echo environment variables with examples.
PowerShell print environment variables using dir env:
dir env: in PowerShell print environment variables on console as below with environment name and environment variable value path
PowerShell echo environment variables using gci env:
It will echo all environment variables on PowerShell console as below
Cool Tip: How to get permissions on folders and subfolders!
PowerShell Print all environment variables using ls env:
ls env: command in PowerShell print all environment variables on console as below
Cool Tip: Use set-aduser to modify active directory user attributes!
PowerShel print env variable
To get PowerShell environment variable value using environment name, use below command
In the above command, it takes APPDATA as environment variable name and prints PowerShell environment variable value as C:\Users\ShellGeek\ AppData\Roaming as environment variable path.
PowerShell Print all environment variables values to file
gci env: | sort-object name| Export-Csv -Path D:\env_variables.txt -NoTypeInformation
The above command gets all environment variable with their name and values sort by name field, and using export-csv, it prints all environment variables to file.
Cool Tip: Know more about how to get aduser using userprincipalname!
PowerShell print all environment variables using GetChildItem Env
Get-ChildItem Env: | Sort Name
Above Get-ChildItem Env: command in PowerShell, print all environment variables.
Sort cmdlet takes the output of the Get-ChildItem command and sorts the list of PowerShell environment variables by the variable name.
PowerShell – Print one environment variable value
In the above example, Get-ChildItem command print environment variable value for APPDATA variable name.
Cool Tip: Use PowerShell script to restart printer spooler service!
Environment variable in Windows operating system stores that is used by programs. Using PowerShell, we can print environment variables on the console.
In the above post, I explained how to use different PowerShell commands like dir env: , gci env: , ls env:, and Get-ChildItem to print environment variables on console or output to file as well.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on ShellGeek home page.