Skip to Content

How to Display the Contents of an Environment Variable from the Command Prompt in Windows

This article explains how to display the contents of an environment variable from the command prompt in Windows using the echo command.

  • An environment variable is a named object that stores data used by applications or the operating system. To display the value of an environment variable, you need to enclose it in percent signs (%). For example, echo %PATH%.
  • You can also use wildcards (*) to display all environment variables that match a certain pattern. For example, echo %P*. To display all environment variables and their values, you can use the set command without any arguments. For example, set.
  • You can also use the command prompt to set, modify, and delete environment variables using the setx command. You need to specify whether the variable is a user variable or a system variable using the /m switch. For example, setx MYVAR Hello /m.

If you want to check the value of an environment variable in Windows, you can use the command prompt to display it. An environment variable is a named object that stores data used by applications or the operating system. For example, the PATH variable contains a list of directories where executable files are located.

In this article, you will learn how to display the contents of an environment variable from the command prompt in Windows using some simple commands and examples. You will also learn how to set, modify, and delete environment variables using the command prompt.

How to Display the Contents of an Environment Variable from the Command Prompt in Windows

What is the Command Prompt?

The command prompt is a text-based interface that allows you to interact with your computer by typing commands. You can use the command prompt to perform various tasks, such as launching programs, managing files and folders, troubleshooting issues, and more.

To open the command prompt in Windows, you can use one of the following methods:

  1. Press Windows + R keys to open the Run dialog box, type cmd, and press Enter.
  2. Press Windows + X keys to open the Power User menu, and select Command Prompt or Command Prompt (Admin).
  3. Type cmd in the Windows search box, and click on Command Prompt from the results.

How to Display the Contents of an Environment Variable from the Command Prompt

To display the contents of an environment variable from the command prompt, you need to use the echo command. The echo command prints a message or a value to the screen. To display the value of an environment variable, you need to enclose it in percent signs (%). For example, if you want to see the value of the PATH variable, you can type:

echo %PATH%

This will show something like this:

C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Git\cmd

You can also use wildcards (*) to display all environment variables that match a certain pattern. For example, if you want to see all environment variables that start with P, you can type:

echo %P*

This will show something like this:

PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PATH=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Git\cmd
PROCESSOR_ARCHITECTURE=AMD64
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 58 Stepping 9, GenuineIntel
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=3a09
PROGRAMDATA=C:\ProgramData
PROGRAMFILES=C:\Program Files
PROGRAMFILES(X86)=C:\Program Files (x86)
PROMPT=$P$G
PSMODULEPATH=C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules
PUBLIC=C:\Users\Public

If you want to see all environment variables and their values, you can use the set command without any arguments. For example, if you type:

set

This will show something like this:

ALLUSERSPROFILE=C:\ProgramData
APPDATA=C:\Users\User\AppData\Roaming
CommonProgramFiles=C:\Program Files\Common Files
CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
CommonProgramW6432=C:\Program Files\Common Files
COMPUTERNAME=DESKTOP-1234567
ComSpec=C:\Windows\system32\cmd.exe
HOMEDRIVE=C:
HOMEPATH=\Users\User
LOCALAPPDATA=C:\Users\User\AppData\Local
LOGONSERVER=\\DESKTOP-1234567
NUMBER_OF_PROCESSORS=4
OS=Windows_NT
...

How to Set, Modify, and Delete Environment Variables from the Command Prompt

You can also use the command prompt to set, modify, and delete environment variables. However, you need to be aware of two types of environment variables: user variables and system variables.

User variables are specific to each user account and are stored in the user profile. System variables are global to all users and are stored in the registry.

To set, modify, or delete a user variable from the command prompt, you need to use the setx command. The setx command creates or modifies a user or system environment variable permanently. The syntax of the setx command is:

setx variable value [/m]

Where variable is the name of the environment variable, value is the value to assign to it, and /m is an optional switch that specifies that the variable should be set in the system environment instead of the user environment.

For example, if you want to create a user variable named MYVAR with the value Hello, you can type:

setx MYVAR Hello

This will create the MYVAR variable in the user environment and display a message like this:

SUCCESS: Specified value was saved.

To verify that the variable was created, you can type:

echo %MYVAR%

This will show something like this:

Hello

Note that the setx command does not affect the current command prompt session. You need to open a new command prompt window to see the changes.

If you want to modify the value of an existing user variable, you can use the same setx command with a different value. For example, if you want to change the value of MYVAR to World, you can type:

setx MYVAR World

This will overwrite the previous value of MYVAR and display a message like this:

SUCCESS: Specified value was saved.

To verify that the variable was modified, you can type:

echo %MYVAR%

This will show something like this:

World

If you want to delete a user variable, you can use the setx command with an empty value. For example, if you want to delete MYVAR, you can type:

setx MYVAR ""

This will remove the MYVAR variable from the user environment and display a message like this:

SUCCESS: Specified value was deleted.

To verify that the variable was deleted, you can type:

echo %MYVAR%

This will show something like this:

%MYVAR%

To set, modify, or delete a system variable from the command prompt, you need to use the setx command with the /m switch. For example, if you want to create a system variable named SYSVAR with the value Test, you can type:

setx SYSVAR Test /m

This will create the SYSVAR variable in the system environment and display a message like this:

SUCCESS: Specified value was saved.

To verify that the variable was created, you can type:

echo %SYSVAR%

This will show something like this:

Test

Note that you need administrative privileges to modify or delete system variables. You can run the command prompt as an administrator by right-clicking on it and selecting Run as administrator.

Frequently Asked Questions (FAQs)

Question: How do I display the contents of multiple environment variables at once?

Answer: You can use the echo command with multiple variables separated by spaces. For example, if you want to display the values of PATH, PATHEXT, and PROCESSOR_ARCHITECTURE variables, you can type:

echo %PATH% %PATHEXT% %PROCESSOR_ARCHITECTURE%

This will show something like this:

C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Git\cmd .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC AMD64

Question: How do I display the contents of an environment variable in a file?

Answer: You can use the redirection operator (>) to send the output of a command to a file. For example, if you want to save the value of PATH variable in a file named path.txt, you can type:

echo %PATH% > path.txt

This will create a file named path.txt in your current directory with the value of PATH variable as its content.

Question: How do I display the contents of an environment variable in another program?

Answer: You can use the substitution operator (&) to run multiple commands in one line. For example, if you want to display the value of PATH variable in Notepad, you can type:

echo %PATH% & notepad

This will show the value of PATH variable in the command prompt and then open Notepad.

Summary

In this article, you learned how to display the contents of an environment variable from the command prompt in Windows using some simple commands and examples. You also learned how to set, modify, and delete environment variables using the command prompt.

Disclaimer: This article is for informational purposes only and does not constitute professional advice. The author and publisher are not responsible for any errors or omissions, or for any consequences arising from the use of the information in this article. Always consult a qualified professional before making any decisions based on the information in this article.