Language: EN

variables-entorno-windows

What are and how to use Windows environment variables

Environment variables are a very useful tool that Windows provides us. However, in general, they are not known or used by most users. So let’s give them a little attention!

Environment variables are simply a text that we access by a name. These exist globally on a machine or in a session, and are one of the ways to save settings.

Among the values we have by default, we have some useful information such as the user’s login, the computer name, or the installation path. Although we will see that it is also possible to create our own.

Environment variables are not an exclusive feature of Windows. They also exist, for example, in Linux. However, they are usually more unknown and forgotten in Windows.

And what are they for? For many things… keep reading 👇 and we’ll see.

What are environment variables for

The information contained in environment variables is very useful in maintenance tasks, script creation, or task automation. We can access them from almost anywhere. For example, if we run this in a Windows terminal, we will see that it shows the name of your computer (as expected).

echo %computername%

But we can access them from many more places, not just in the terminal. For example, in the “Run” menu.

windows-system-variables-run

For example, it is possible to use them in the Windows file explorer bar.

windows-system-variables-explorer

You could even do it in the middle of a “save document” dialog box. Which is very useful, when we see in the end how to create your own variables.

Practical example. I connect to a remote computer and I want to go to its user folder.

Default environment variables

By default, Windows provides many predefined environment variables that we can use at any time. Here are some of them.

Session information

  • %USERNAME%: returns the name of the user currently connected to the system.
  • %USERDOMAIN%: returns the name of the domain to which the user currently connected to the system belongs.
  • %COMPUTERNAME%: returns the name of the computer.
  • %OS%: returns the name of the operating system you are working on.
  • %LOGONSERVER%: returns the name of the logon server that authenticated the current user session.

User directories

  • %HOMEDRIVE%: drive on which the current user’s profile folder is located.
  • %HOMEPATH%: profile folder of the current user.
  • %USERPROFILE%: folder of the current user’s profile.
  • %ALLUSERSPROFILE%: leads to the public system profile.
  • %APPDATA%: application data folder for the current user.
  • %LOCALAPPDATA%: local application data folder for the current user.

System directories

  • %WINDIR% or %SYSTEMROOT%: Windows installation directory (usually “C:\Windows”).
  • %PROGRAMFILES%: directory where programs are installed for all users of the system.
  • %PROGRAMFILES(X86)%: directory where 32-bit programs are installed on 64-bit operating systems.
  • %PUBLIC%: public system folder.
  • %TEMP% or %TMP%: temporary folder for the current user.

More variables

  • %PATH%: shows a semicolon-separated list of the paths in which executable files are searched when you run a command in the command line or in a script.
  • %PATHEXT%: shows a semicolon-separated list of file extensions that are considered executable files when running commands in the command line or in a script.
  • %TIME%: this variable

Personal environment variables

We can also create our own environment variables. The easiest way to do this is to search for “Environment variables” in the start menu and you will see this

windows-system-variables-panel-control

There you click on the “Variables” button.

windows-system-variables-create

And you will see a list with your variables, both at the user level and at the machine level.

windows-system-variable-edit

Alternatively, we can also set a variable with the setx command with the following syntax, but I do not advise you to do this unless you have to do it in a script

setx [variable_name] "[variable_value]"

setx is limited to a length of 1024 characters. If you use it for longer text, it will truncate it. In particular, NEVER use it with the PATH variable

Here is where things get more interesting. Imagine the number of places you can use them. For example

  • In systems administration, putting a variable of the computers or sessions
  • Create shortcuts for paths, or URLs you frequently access
  • Create aliases for commands you frequently execute
  • Save parameters of goodness knows what

Don’t go overboard and start creating an environment variable. But 2 or 3 well created ones can save you a lot of time and simplify your life. Until next time!