Environment Variables
Environment variables on Windows are global variables that are NOT case sensitive and CAN have spaces and numbers in the name. They have the following format:
# cmd only
%ENV_VAR%
Local variables.
set LOCAL_VAR=test
echo %LOCAL_VAR%
test
Environment variables can be separated into their defined scopes: System and User. There is also the Process scope, but it is volatine by nature and it is considerd to be a subscope of both the System and User scopes.
System - Environment variabled defined by the OS and are accessible globally by all users and accounts that log on to the system. The OS requires these variables to function properly and are loaded upon runtime. Their registry location is at
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
.User - Environment variables defined by the currently active user and are only accessible to them. Stored at
HKEY_CURRENT_USER\Environment
.Process - Environment variables that are defined and accessible in the context of the currently running process. Due to their transient nature, their lifetime only lasts for the currently running process in which they were initially defined. They also inherit variables from the System/User Scopes and the parent process that spawns it (only if it is a child process). They don't have a registry location as they are stored in process memory.
Display Values
Display the value of an environment variable.
# display all environment variables
set
# display the value of a specific environment variable
set %PATH%
# or
echo %PATH%
Managing
set
modifies an environment variable temporarily, i.e., in the current command line session. setx
makes registry changes which are permanent. They have almost identical syntax.
Create/Change
set DCIP=172.16.10.10
echo %DCIP%
setx DCIP 172.16.10.10
Delete
setx DCIP ""
set %DCIP%
Environment variable %DCIP% not defined
Important Variables
%PATH%
Specifies a set of directories where executable programs are located.
%OS%
The current operating system on the user's workstation.
%SYSTEMROOT%
Expands to C:\Windows
. A system-defined read-only variable containing the Windows system folder. Anything Windows considers important to its core functionality is found here, including important data, core system binaries, and configuration files.
%LOGONSERVER%
Provides us with the login server for the currently active user followed by the machine's hostname. We can use this information to know if a machine is joined to a domain or workgroup.
%USERPROFILE%
Provides us with the location of the currently active user's home directory. Expands to C:\Users\{username}
.
%ProgramFiles%
Equivalent of C:\Program Files
. This location is where all the programs are installed on an x64
based system.
%ProgramFiles(x86)%
Equivalent of C:\Program Files (x86)
. This location is where all 32-bit programs running under WOW64
are installed. Note that this variable is only accessible on a 64-bit host. It can be used to indicate what kind of host we are interacting with. (x86
vs. x64
architecture)
Last updated