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 store information about the operating system environment information and programs.

This information includes the operating system path, the 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 to show environment variables to the console:

  • $Env:
  • dir env:
  • gci env:
  • ls env:
  • Get-ChildItem env:

In the below image, dir:env in PowerShell, print environment variables to the 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:

In the above example, dir env: command in PowerShell print environment variables on the 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:

In the above example, the Get-ChildItem alias gci uses the env: to 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 List all environment variables using ls env:

ls env:

The above ls env: command in PowerShell prints all environment variables on the console.

The output of the above command in Powershell shows the environment variables with their Name and Value.

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 the environment name, use the below command

$env:APPDATA 

In the above example, $env uses APPDATA as the 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 the Export-CSV command, it prints all environment variables to the file specified by the Path parameter.

Cool Tip: Know more about how to get an aduser using userprincipalname!

PowerShell prints all environment variables using GetChildItem Env

Get-ChildItem Env: | Sort Name

The 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.

The output of the above command in PowerShell displays the environment variables with their name and value sorted by the property 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 the environment variable value for the APPDATA variable name.

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

Conclusion

Environment variable in the 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 the 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.