cmd Viewing environment variables


Viewing environment variables in the Windows Command Prompt (cmd) is an important aspect of system configuration and management. Environment variables are dynamic values that can affect the way running processes behave on a computer. They can store information like system paths, user preferences, and system settings. Here’s how you can view and work with environment variables in cmd, along with examples and expected outputs.

1. Using the set Command

The set command displays all environment variables and their values in the current command prompt session.

Basic Syntax:

set

Example:

To view all environment variables, simply enter:

set

Output:

C:\Users\YourUsername>set ALLUSERSPROFILE=C:\ProgramData APPDATA=C:\Users\YourUsername\AppData\Roaming CommonProgramFiles=C:\Program Files\Common Files ComputerName=YOUR-PC ComSpec=C:\Windows\system32\cmd.exe HOMEDRIVE=C: HOMEPATH=\Users\YourUsername LocalAppData=C:\Users\YourUsername\AppData\Local Path=C:\Program Files\Java\jdk-14\bin;C:\Windows\System32;... PROMPT=$P$G TEMP=C:\Users\YourUsername\AppData\Local\Temp USERNAME=YourUsername

2. Using the echo Command

You can also use the echo command to display the value of a specific environment variable by referencing it with the % symbol.

Basic Syntax:

echo %VARIABLE_NAME%

Example:

To view the value of the PATH environment variable, enter:

echo %PATH%

Output:

C:\Users\YourUsername>echo %PATH% C:\Program Files\Java\jdk-14\bin;C:\Windows\System32;C:\Windows;...

3. Using the set Command with a Variable Name

You can use the set command with a variable name to display only the value of that specific variable.

Basic Syntax:

set VARIABLE_NAME

Example:

To display the value of the TEMP variable, enter:

set TEMP

Output:

C:\Users\YourUsername>set TEMP TEMP=C:\Users\YourUsername\AppData\Local\Temp

4. Using the reg Command to View User Environment Variables

You can also access environment variables stored in the registry using the reg command.

Basic Syntax:

reg query "HKEY_CURRENT_USER\Environment"

Example:

To view user-defined environment variables, enter:

reg query "HKEY_CURRENT_USER\Environment"

Output:

C:\Users\YourUsername>reg query "HKEY_CURRENT_USER\Environment" HKEY_CURRENT_USER\Environment PATH REG_SZ C:\Program Files\Java\jdk-14\bin;C:\Windows\System32;... TEMP REG_SZ C:\Users\YourUsername\AppData\Local\Temp

Summary

Viewing environment variables in the Windows Command Prompt is essential for troubleshooting and managing system configurations. You can use the set command to display all variables or specific ones, the echo command to retrieve the value of a particular variable, and the reg command to access variables stored in the Windows registry. Understanding how to view and manipulate environment variables can greatly assist in system administration and development tasks.