How to list all environment variables ?


👤 Diwas Poudel    🕒 Sep 21 2021    📁 Fix    📜 0 comment

In programming, variables are the location that holds a value, and value can be anything. It can be a filename, or number, or any other data. In the same way, there are Environment Variables in your Operating System. Environment Variables are global variables that hold values related to the current working environment and can be used by the operating system and other applications to get necessary OS information
or Environment Information. Environment Variables are available in Windows, Mac, and Linux.

They are formed as number/value pairs and those values can be access by the OS and application as needed.General Format of environment value looks like this:

Variable1 = C:\xyz
Variable2 = 6
variable2 ='val1:val2'
variable4 = val1;val2

...

These types of environment variables can be viewed, created, edited, and deleted. Environment Variable in general helps applications to know which directory to install installable files, where to find user and system settings.

Some of the environment variables are PATH, USER, HOME, UID, SHELL, TERM, EDITOR, etc.PATHTEXT, OS, TEMP,SYstemRoot.PATH is a popular Environment variable that is available in Windows, Mac, and Linux.

If you want to know what environment variables are used in various OS then I will guide you for this.

List of Environment variable in Windows Environment 

There is various way to list environment variables in windows. Let's discuss some of them.

1) List environment variables using Command Line

In command prompt type :

SET

and press Enter.

set-environment-variable
fig. List all windows environment variable

Some of the most common environment variables:

AppData: AppData contains data and settings of various installed programs on your PC.

COMPUTERNAME: Unique identifier is given to your computer

HOMEPATH: HomePath describes the path for user personal files. It contains user files like download, Desktop, Documents, Videos, Picture, Contacts, and many more.

PATH: The PATH variable provides the directories on the machine where executable programs can be found.

If you have a large number of system and user variables in environment variables then SET display all at a time. But if you want one page and then one line at a time then try this:

SET | more

set-pipe-more

If you want to save this variable for a future look then you can save it in a text file with the following command.

SET > any_name.txt
fig. output.txt contains an environment variable path

Here, I have used output.txt as file name and this file get saved under C:\User\Dell path and contain output.txt look like this:

output-environmentvariable

If you want help and wants to know more about set cmdlet then type the below command in command prompt for help:

set /?
Note: All commands used here are case insensitive. So, Get-ChildItem Env: is equivalent to get-childitem env:

2) List environment variables using Powershell

Get-ChildItem (GCI) is a useful cmdlet that is used to get items and, if the item is a container, it will get child items that are available inside the container.

You can simply type below one line to get all environment variables.

Get-ChildItem Env:

or Simply

gci env:

powershell-environmentvariable

As you can see, some lines of the output are truncated. If you want to get the full output without any truncation, use the cmdlet below.

gci Env: | Format-Table -Wrap -AutoSize

or

Get-ChildItem Env: | Format-Table -Wrap -AutoSize

The gci env: cmdlet retrieves all environment variables, which are then passed through the pipeline(|) to Format-Table and displayed in a table. The AutoSize option adjusts column widths to avoid truncation.-Wrap Displays text that exceeds the column width in the new line without truncation.

Below, we can see that no lines in the output are truncated, as they were previously.

gci-cmdlet-without-truncation

3 Using reg query command in Command Prompt

If you just want System variables then type below in the command prompt

reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"

If you just want User variables then type below in the command prompt.

reg query HKEY_CURRENT_USER\Environment

4 In Linux 

You can use the below cmdlet in Linux for getting environment variables. I am testing in ubuntu and it must work in other distributions of Linux.

printenv

ubuntu-machine

5 In macOS

For macOS, we can use printenv for displaying all environment variables.

printenv