Print Environment Variables in Windows PowerShell

  1. What Is an Environment Variable?
  2. Using the PowerShell Environment Variable
  3. Printing All Environment Variables Using the Get-ChildItem Command
Print Environment Variables in Windows PowerShell

Environment variables can affect how running processes will behave on a computer. Windows PowerShell can access, manage, or change environment variables.

This article will focus on getting and outputting the environment variables and printing them on the command-line terminal.

What Is an Environment Variable?

Environment variables, depicted by the variable Env: in Windows PowerShell, store information about the operating system environment and programs. This information details include the operating system path, location of the windows installation directory, number of processes used by the operating system, and so much more.

In Windows PowerShell, we can use the following commands to print or get the environment variables.

Using the PowerShell Environment Variable

To get the PowerShell Environment Variable, we can call the variable $Env: and specify the environment variable to be printed.

In the example below, we used the PATH environment variable. The PATH variable is your operating system’s system environment variable to locate executables from the command line interface.

Example Code:

$env:PATH

Output:

C:\Windows\system32;C:\Windows;C:\Users\user01\AppData\Local\Microsoft\WindowsApps;

Printing All Environment Variables Using the Get-ChildItem Command

We can use the Get-ChildItem cmdlet to output all environment variables to the command-line interface.

Get-ChildItem Env:

Since the Get-ChildItem cmdlet is a native PowerShell command, we can use it with other PowerShell commands through piping.

Get-ChildItem Env: | Select Name | Export-Csv -Path C:\env_variables.txt -NoTypeInformation

We can also output a specific environment variable using the native Get-ChildItem command. Try running the example command below to output the %APPDATA% file path.

Get-ChildItem Env:APPDATA

Note that the commands gci, ls, and dir are aliases to the cmdlet Get-ChildItem.

Example Code:

Get-Alias -Definition Get-ChildItem

Output:

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           dir -> Get-ChildItem
Alias           gci -> Get-ChildItem
Alias           ls -> Get-ChildItem

For this reason, we can use the commands dir, gci, and ls in exchange for the native cmdlet Get-ChildItem.

Example Code:

dir env:
gci env: | select name
ls env:ALLUSERSPROFILE | Export-Csv -Path C:\env_variables.txt -NoTypeInformation
Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn

Related Article - PowerShell Environment Variables