Home » PowerShell » PowerShell – Print Environment Variables

PowerShell – Print Environment Variables

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, and a 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

  • $Env:
  • 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.

PowerShell - Show Environment Variables on Console
PowerShell env – Print Environment Variables on Console

In this tutorial, I will explain how to print environment variables or echo environment variables using PowerShell.

PowerShell Tip: dir, gci and ls are the PowerShell Get-ChildItem aliases to show environment variables.

Get-ChildItem alias to echo environment variables
PowerShell – Get-ChildItem alias to echo 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:

Above command dir env: in PowerShell print environment variables on console as below with environment name and environment variable value path

PowerShell Print environment variables
PowerShell Print environment variables

PowerShell echo environment variables using gci env:

gci env:

It will echo all environment variables on the PowerShell console as below

PowerShell - gci env: show environment variables
PowerShell – gci env: show environment variables

Cool Tip: How to get permissions on folders and subfolders!

PowerShell Print all environment variables using ls env:

ls env:

Above ls env: command in PowerShell print all environment variables on console as below

PowerShell - ls env: Prints all environment variables
PowerShell – ls env: print all environment variables

Cool Tip: Use set-aduser to modify active directory user attributes!

PowerShell print env variable

To get PowerShell environment variable value using environment name, use the below command

$env:APPDATA 

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.

Cool Tip: How to set environment variable using PowerShell!

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 variables with their name and values sorted by name field, and using Export-CSV, it prints all environment variables to the 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.

Cool Tip: How to refresh environment variable in PowerShell!

PowerShell – Print one environment variable value

Get-ChildItem Env:APPDATA 

In the above example, the Get-ChildItem command prints environment variable value for APPDATA variable name.

Cool Tip: Use PowerShell script to restart printer spooler service!

Conclusion

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 the ShellGeek home page.

Leave a Comment