Windows read environment variables

What is an environment variable in Windows? An environment variable is a dynamic “object” containing an editable value which may be used by one or more software programs in Windows.

In this note i am showing how to list environment variables and display their values from the Windows command-line prompt and from the PowerShell.

Cool Tip: Add a directory to Windows %PATH% environment variable! Read More →

The environment variables in Windows can be printed using the Windows command-line prompt (CMD) or using the PowerShell.

Windows Command-Line Prompt (CMD)

List all Windows environment variables and their values:

C:\> set

“Echo” the contents of a particular environment variable:

C:\> echo %ENVIRONMENT_VARIABLE%

Windows PowerShell

Print all Windows environment variables (names and values):

PS C:\> gci env:* | sort-object name

Show the contents of a particular environment variable:

PS C:\> echo $env:ENVIRONMENT_VARIABLE

Cool Tip: Set environment variables in Windows! Read More →

Was it useful? Share this post with the world!

Переменные окружения (среды) в Windows содержат различную информацию о настройках системы и среды пользователя. Различают переменные окружения пользователя, системы и процессов.

Самый простой способ просмотреть содержимое переменных окружения в Windows – открыть свойства системы (sysdm.cpl) -> Дополнительно -> Переменные среды. Как вы видите, в открывшемся есть две секции: в верхней содержатся переменные окружения пользователя, в нижнем – системные.

переменные окружения Windows

Кроме того, переменные среды хранятся в реестре системы. Пользовательские переменные хранятся в разделе HKEY_CURRENT_USER\Environment. Системные – в HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment.

HKEY_CURRENT_USER\Environment

Вывести значения всех переменных окружения можно в командной строке Windows. Команда простая:

Set

команда Set

Команда выведет список переменных среды и их значения.

В PowerShell для вывод всех переменных окружения можно использовать команду:

ls env:

Если нужно вывести значение только одной переменной, нужно воспользоваться командой echo, причем имя переменной нужно заключить в знаки процентов. Например,

Echo %systemroot%

Чтобы сохранить все переменные среды и их значения в текстовый файл, воспользуйтесь командой:

set > c:\tmp\env_var.txt

список переменных окружения в текстовом виде

Переменные окружения конкретного процесса можно получить с помощью бесплатной утилиты Process Explorer (от Sysinternals). Достаточно открыть свойства процесса и перейти на вкладку Environment.

Process Explorer - переменные окружения процесса

To open environment variables from the Command Prompt (cmd), you can use the `set` command to view all environment variables or `echo` command for a specific variable, such as the `PATH` variable.

echo %PATH%

Understanding Environment Variables

What are Environment Variables?

Environment variables are dynamic values that the operating system and applications use to determine various settings and paths. They provide a way to configure the environment in which programs run and can significantly affect how applications interact with the system. Common environment variables include:

  • PATH: Indicates the directories that the shell searches for executable files.
  • TEMP: Specifies the directory where temporary files are stored, often used by applications for a short duration.

Understanding these variables is crucial for troubleshooting applications and configuring system settings.

Why Use Environment Variables?

Using environment variables allows for a flexible and dynamic way to configure your system. They can define user preferences, control the behavior of programs, or provide crucial paths needed for certain tasks. For instance, if you are developing software, you may need to adjust the PATH variable to include the directory of your programming tools.

Check Environment Variables in Cmd: A Quick Tutorial

Check Environment Variables in Cmd: A Quick Tutorial

Accessing Environment Variables through CMD

Opening Command Prompt

To begin working with environment variables through CMD, you first need to open the Command Prompt. Here’s how to do it:

  1. Press Win + R to open the Run dialog.
  2. Type cmd and hit Enter or click OK.
  3. Alternatively, you can search for Command Prompt in the Start menu.

Once the Command Prompt opens, you are ready to interact with your environment variables.

Viewing Current Environment Variables

Understanding the current environment variables set in your system can be very useful. You can use the following commands to view them:

Using the `SET` Command

The `SET` command displays all environment variables and their values. To see a complete list, simply type:

set

Running this command will output all the environment variables currently available in your session.

Using the `ECHO` Command

If you’re interested in viewing a specific environment variable, the `ECHO` command is a great way to do that. For example, to view the PATH variable, enter:

echo %PATH%

This command will print out the value of the PATH environment variable on your screen, helping you to understand which directories are included for executable files.

Getting a Specific Environment Variable

If you want to access another specific variable, such as TEMP, you can use the `ECHO` command in the same fashion:

echo %TEMP%

This will output the path of the temporary files directory. Understanding these specific variable values can help with application performance and configuration.

View Environment Variables Cmd with Ease

View Environment Variables Cmd with Ease

Opening Environment Variables Graphically

Accessing Through System Properties

While using CMD is efficient, sometimes a graphical interface can be more intuitive, especially for users unfamiliar with command-line operations. To access the environment variables graphically:

  1. Right-click on This PC or My Computer on your desktop or in the File Explorer.
  2. Select Properties.
  3. Click on Advanced system settings on the left sidebar.
  4. Under the System Properties window, click the Environment Variables button.

This interface provides a user-friendly way to view and edit user and system environment variables.

cmd Echo Environment Variable: Quick Start Guide

cmd Echo Environment Variable: Quick Start Guide

Using the `SET` Command

If you need to temporarily set an environment variable that will only last for the current session, you can use the `SET` command. For example, to set a variable named MY_VAR, you would type:

set MY_VAR=HelloWorld

This command creates a new environment variable called MY_VAR with the value HelloWorld for the duration of this CMD session. Keep in mind that once you close the CMD window, the variable will be lost.

Using the `SETX` Command for Permanent Changes

For those looking to make permanent changes that persist even after closing the CMD window, `SETX` is the command to use. This command allows you to set environment variables that remain available across system reboots. For instance, to create or modify the MY_VAR variable permanently, you would execute:

setx MY_VAR "HelloWorld"

Note that when using `SETX`, you won’t see immediate output in the current session. Instead, it will take effect in new CMD instances or after a restart.

How to Open Services from Cmd in a Flash

How to Open Services from Cmd in a Flash

Best Practices when Working with Environment Variables

Safety Precautions

Adjusting environment variables can significantly impact system behavior, so it’s wise to back up existing variables before making changes. To do this, simply copy the current variable values before modifying them, as this can prevent unwanted errors or issues later.

Common Mistakes to Avoid

One common pitfall when using CMD for environment variable management is forgetting the percentage signs around variable names when using the `ECHO` command. For example, `echo PATH` will not work as expected; it should be `echo %PATH%`. Additionally, ensure that any values with spaces are enclosed in quotes when using `SETX`.

How to Open Control Panel from Cmd: Quick Steps Explained

How to Open Control Panel from Cmd: Quick Steps Explained

Conclusion

Through this guide, you have learned how to open environment variables from cmd and manage them effectively. Whether you’re viewing, modifying, or understanding these dynamic values, mastering CMD commands can enhance your productivity and system configuration skills.

Feel free to practice these commands and explore different environment variables to see how they impact your Windows experience! For ongoing tips and tricks related to CMD, don’t hesitate to follow our resources for more insights.

How to Open Another Drive in Cmd Easily

How to Open Another Drive in Cmd Easily

Additional Resources

Recommended Reading

For more in-depth learning, consider checking out additional documentation on CMD commands and environment variables available through Microsoft’s official resources or other programming community websites.

Community and Support

Engaging with forums and communities dedicated to CMD users can provide assistance and additional insight. Join discussions on platforms like Stack Overflow or dedicated programming forums to connect with like-minded individuals.

How to Open PowerShell from Cmd in Simple Steps

How to Open PowerShell from Cmd in Simple Steps

FAQs

What if I can’t find my environment variables?

If you’re unable to see your environment variables, ensure you’re using an elevated CMD (Run as Administrator) for system-level variables. You can also check the graphical interface via System Properties to confirm their existence.

How do I restore default environment variables?

If you’ve made changes that you want to revert, you can either set the environment variables back to their original values manually or, if applicable, use system restore points to revert your system settings.

In computer programming, variables are the storage locations for values, and the values themselves can be anything. It could be a number, a filename, or some other piece of data. In a similar fashion, there are things called Environment Variables in your Operating System.

Environment Variables are global variables that store values linked to the current working environment and can be utilized by the operating system and other applications to retrieve necessary operating system information. Environment Variables are also known as env variables. Environment Variables are available in Windows, Mac, and Linux.

Table of Contents

  • List of Environment variables in Windows Environment 
    • List environment variables using Command Line
    • List environment variables using Powershell
    • Using reg query command in Command Prompt
  • List of Environment variables in Linux Environment  
  • List of Environment variables in macOS Environment  
  • Conclusion:

They are created as pairs consisting of a number and a value, and both the operating system and the application are able to access those values. The 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, and 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 on this.

List of Environment variables in Windows Environment 

There are actually two types of environment variables in Windows. They are :

1. Local/User Environment Variable: User Variables store information that is unique to the logged-in user and are only relevant during the current session. They have located under the «HKEY_CURRENT_USER/Environment» path in the Registry

2. System Environment Variable: Because they are set uniformly across all of your computer’s users, system variables don’t contain any user-specific data. All user accounts are affected by these global variables.They are located under «HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/ControlSession /ManagerEnvironment» path in Registry

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

List environment variables using Command Line

In the command prompt type :

SET

and press Enter.

set-environment-variable

fig. List all windows environment variable

Some of the most common environment variables:

Environment Variables Description
%APPDATA% AppData contains data and settings of various installed programs on your PC.
%COMPUTERNAME% The unique identifier is given to your computer
%HOMEPATH% HomePath describes the path for the user’s personal files. It contains user files like downloads, Desktop, Documents, Videos, Picture, Contacts, and many more.
%PATH% The PATH variable provides the directories on the machine where executable programs can be found.
%CD% Returns the current directory string.
%DATE% The most recent date, formatted according to the Date command.
%LOGONSEVER% This function brings back the name of the domain controller that was responsible for validating the current logon session.
%NUMBER_OF_PROCESSORS% This number indicates how many processors are currently installed on the computer.
%OS% This function returns the name of the operating system.
%PROMPT% The directory in which the operating system is stored is returned by this function.
%WINDIR% The directory in which the operating system is stored is returned by this function.
%USERNAME% This function gets you the name of the user who is logged in at the moment.
%RANDOM% It returns a random decimal number between 0 and 32767. 

If you have a large number of system and user variables in environment variables then SET displays 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 the file name, and this file gets 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:

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

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

List of Environment variables in Linux Environment  

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

In Linux, you can simply use the below command for displaying the list of environment variables.

env

It is not possible to view all bash variables when using the env command since it will only show a list of environment variables that have been exported.

List of Environment variables in macOS Environment  

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

printenv

Conclusion:

In this approach, we may get a list of all the environment variables in major operating systems such as Windows, Linux, and Mac.

If you want to see or check the names and values of Environment Variables in Windows 11 or Windows 10, here is how you can do that. There are mainly four ways to find almost everything about the Environment Variables in Windows 11/10, and this article explains all the possible methods.

What are Environment Variables in Windows 11/10

Environment Variables contain some information related to WindowsApps path, currently logged-in user’s information, Temporary folder’s path, running processes, etc. Although Windows 11/10 comes with a few of those, you can add or modify Environment Variables in Windows PC easily.

However, if you want to find those Environment Variables in Windows 11 or Windows 10, this guide will be handy for you. As said earlier, there are four ways – using the Windows Settings panel, using Command Prompt, using the Registry Editor, and using Windows PowerShell.

To see the names and values of Environment Variables in Windows 11/10, follow these methods:

  1. Using Windows Settings panel
  2. Using Command Prompt
  3. Using Registry Editor
  4. Using Windows PowerShell

To learn more about these steps, continue reading.

1] Using Windows Settings panel

How to see Names and Values of Environment Variables in Windows 11/10

It is the most common method to find the names and values of the Environment Variables in Windows 11/10 PC. Earlier, it was possible to do the same using Control Panel. However, if you are using Windows 11, there is no need to go through the Control Panel since it will eventually open the Windows Settings.

Follow these steps to find the Environment Variables:

  • Press Win+I to open Windows Settings.
  • Go to System > About.
  • Click on the Advanced system settings.
  • Click the Environment Variables button.
  • Find the Environment Variables.

Next, you need to click on each variable to find the details. It displays a detailed list in the System variables box.

2] Using Command Prompt

How to see Names and Values of Environment Variables in Windows 11/10

It is probably the easiest method to find almost everything about the Environment Variables on Windows 11/10 PC. For this purpose, we are going to use the Command Prompt instance in Windows Terminal. However, you can open the standalone Command Prompt window and enter the following command:

set

In case you do not know the process to open the Windows Terminal, you can press Win+X and select the Windows Terminal from the WinX menu.

3] Using Registry Editor

How to see Names and Values of Environment Variables in Windows 11/10

If you do not want to do much with the Environment Variables and want to check them only, you can use the Registry Editor. As these variables depend on the user profile, you need to open the HKEY_CURRENT_USER. To find the names and values of the Environment Variables using Registry Editor, do the following:

  • Press Win+R to open the Run prompt.
  • Type regedit and hit the Enter button.
  • Click on the Yes button in the UAC prompt.
  • Navigate to this path: HKEY_CURRENT_USER\Environment
  • Find the variables.

In case you want to modify the existing variables using Registry Editor, you need to double-click on each of them and change them according.

4] Using Windows PowerShell

How to see Names and Values of Environment Variables in Windows 11/10

Like, Command Prompt, you can use Windows PowerShell to find the same Environment Variables on your Windows 11 or Windows 10 computer. However, it displays all the Environment Variables of all the users, whereas other methods display only the current user’s variables. That being said, the list could be lengthy since it may contain some variables, such as ComSpec, LOCALAPPDATA, HOMEDRIVE, HOMEPATH, ALLUSERSPROFILE, etc.

If you are fine with the lengthy list, you can use Windows PowerShell and enter this command:

dir env:

Like the Command Prompt method, you can open the standalone Windows PowerShell or the PowerShell instance in the Windows Terminal.

How do I view Environment Variables in Windows 11?

To view the Environment Variables in Windows 11, you can use any of the aforementioned methods. If you do not want to modify existing variables, you can use the Command Prompt method. For that, you can open the Command Prompt or the Command Prompt instance in Windows Terminal and enter this command: set. Then, it displays almost everything about the current user’s Environment Variables on your screen. Similarly, you can use the Windows PowerShell method to get the same thing done. In that case, you have to use this command: dir env:

How do I get a list of Environment Variables?

To get a list of Environment Variables, you have four techniques in your hands. However, using the Windows Settings method is probably the most fruitful method since you can view, add, or modify any Environment Variable without any issue. However, if you want to get a list only, you can use any of the other three methods. To choose one, you can open the Windows Terminal and enter this command in the PowerShell instance: dir env:. If you want to use the Command Prompt instance, you need to execute this command: set.

That’s all! Hope this guide helped.

Read: How to add or edit a PATH Environment Variable in Windows.

Понравилась статья? Поделить с друзьями:
0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Старые
Новые Популярные
Межтекстовые Отзывы
Посмотреть все комментарии
  • Iobit unlocker для windows 10 repack
  • Как ускорить озу windows 10
  • Изменение шрифта значков рабочего стола windows 10
  • Перерегистрация в домене windows cmd
  • Как поместить файл в карантин windows 10