- How to edit environment variables in Windows 10
- Editing Environment Variables:
- Navigating to Environment Variables Manually
- About Environment Variables
- SHORT DESCRIPTION
- LONG DESCRIPTION
- Using and changing environment variables
- Environment variables that store preferences
- Managing environment variables
- Using the Environment provider
- Using Item cmdlets
- Saving changes to environment variables
- Using System.Environment methods
- Win32_Environment class
- Syntax
- Members
- Properties
- Remarks
- Examples
How to edit environment variables in Windows 10
Environment variables are the values that contain all the information regarding system environment. Every process inside Windows OS has a block that contains a set of environment variables and their values. There are two kinds of environment variables i.e. User Environment Variables and System Environment Variables. As the name represents, user environment variables are set for every user while system environment variables are for everyone who uses the system.
Any user working on command prompt or batch scripts will be well aware of environment variables because of the repetition of a value again and again. Every environment variable has two parameters i.e. name and value. So, you can edit or create your own environment variables in order to ease the work for yourself. So, in this guide, I will discuss about the way to edit environment variables in Windows 10.
Editing Environment Variables:
In order to edit environment variables, follow the steps below to access them.
Navigating to Environment Variables Using Command Prompt:
1. If you want to have access to both the variables i.e. user and system variables, then, open an elevated Command Prompt by right clicking over the Start Menu icon and selecting Command Prompt (Admin) from the list.
2. While inside the command prompt, type or copy/paste the following command stated below. You can paste the command by right clicking inside Command prompt and selecting Paste. Hit the Enter key to execute the command.
Command: rundll32.exe sysdm.cpl,EditEnvironmentVariables
3. After hitting Enter, Environment Variables window will pop-up with a section for User variables and another section for System variables.
4. In order to edit a variable, select that variable and press the Edit button to change the Name or Press OK afterwards.
5. If you want to create a new environment variable for yourself, then, click over the New button and type the Name and Value for that particular variable followed by the OK
Navigating to Environment Variables Manually
1. Press Win+ X keys on the keyboard and select System from the list.
2. Inside the System window, click on Advanced system settings located at the left pane.
3. Inside the advanced system settings, click on the Environment Variables button at the bottom and do follow the same procedure to edit environment variables as described above.
About Environment Variables
SHORT DESCRIPTION
Describes how to access Windows environment variables in PowerShell.
LONG DESCRIPTION
Environment variables store information about the operating system environment. This information includes details such as the operating system path, the number of processors used by the operating system, and the location of temporary folders.
The environment variables store data that is used by the operating system and other programs. For example, the WINDIR environment variable contains the location of the Windows installation directory. Programs can query the value of this variable to determine where Windows operating system files are located.
PowerShell can access and manage environment variables in any of the supported operating system platforms. The PowerShell environment provider simplifies this process by making it easy to view and change environment variables.
Environment variables, unlike other types of variables in PowerShell, are inherited by child processes, such as local background jobs and the sessions in which module members run. This makes environment variables well suited to storing values that are needed in both parent and child processes.
Using and changing environment variables
On Windows, environment variables can be defined in three scopes:
- Machine (or System) scope
- User scope
- Process scope
The Process scope contains the environment variables available in the current process, or PowerShell session. This list of variables is inherited from the parent process and is constructed from the variables in the Machine and User scopes. Unix-based platforms only have the Process scope.
You can display and change the values of environment variables without using a cmdlet by using a variable syntax with the environment provider. To display the value of an environment variable, use the following syntax:
For example, to display the value of the WINDIR environment variable, type the following command at the PowerShell command prompt:
In this syntax, the dollar sign ( $ ) indicates a variable, and the drive name ( Env: ) indicates an environment variable followed by the variable name ( windir ).
When you change environment variables in PowerShell, the change affects only the current session. This behavior resembles the behavior of the Set command in the Windows Command Shell and the Setenv command in UNIX-based environments. To change values in the Machine or User scopes, you must use the methods of the System.Environment class.
To make changes to Machine-scoped variables, must also have permission. If you try to change a value without sufficient permission, the command fails and PowerShell displays an error.
You can change the values of variables without using a cmdlet using the following syntax:
For example, to append ;c:\temp to the value of the Path environment variable, use the following syntax:
On Linux or MacOS, the colon ( : ) in the command separates the new path from the path that precedes it in the list.
You can also use the Item cmdlets, such as Set-Item , Remove-Item , and Copy-Item to change the values of environment variables. For example, to use the Set-Item cmdlet to append ;c:\temp to the value of the Path environment variable, use the following syntax:
In this command, the value is enclosed in parentheses so that it is interpreted as a unit.
Environment variables that store preferences
PowerShell features can use environment variables to store user preferences. These variables work like preference variables, but they are inherited by child sessions of the sessions in which they are created. For more information about preference variables, see about_preference_variables.
The environment variables that store preferences include:
Stores the execution policy set for the current session. This environment variable exists only when you set an execution policy for a single session. You can do this in two different ways.
Start a session from the command line using the ExecutionPolicy parameter to set the execution policy for the session.
Use the Set-ExecutionPolicy cmdlet. Use the Scope parameter with a value of «Process».
PowerShell provides control over the file that is used to cache data about modules and their cmdlets. The cache is read at startup while searching for a command and is written on a background thread sometime after a module is imported.
Default location of the cache is:
- Windows PowerShell 5.1: $env:LOCALAPPDATA\Microsoft\Windows\PowerShell
- PowerShell 6.0 and higher: $env:LOCALAPPDATA\Microsoft\PowerShell
- Non-Windows default:
The default filename for the cache is ModuleAnalysisCache . When you have multiple instances of PowerShell installed, the filename includes a hexadecimal suffix so that there is a a unique filename per installation.
If command discovery isn’t working correctly, for example Intellisense shows commands that don’t exist, you can delete the cache file. The cache is recreated the next time you start PowerShell.
To change the default location of the cache, set the environment variable before starting PowerShell. Changes to this environment variable only affect child processes. The value should name a full path (including filename) that PowerShell has permission to create and write files.
To disable the file cache, set this value to an invalid location, for example:
This sets the path to the NUL device. PowerShell can’t write to the path but no error is returned. You can see the errors reported using a tracer:
When writing out the module analysis cache, PowerShell checks for modules that no longer exist to avoid an unnecessarily large cache. Sometimes these checks are not desirable, in which case you can turn them off by setting this environment variable value to 1 .
Setting this environment variable takes effect immediately in the current process.
The $env:PSModulePath environment variable contains a list of folder locations that are searched to find modules and resources.
By default, the effective locations assigned to $env:PSModulePath are:
System-wide locations: These folders contain modules that ship with PowerShell. The modules are store in the $PSHOME\Modules location. Also, This is the location where the Windows management modules are installed.
User-installed modules: These are modules installed by the user. Install-Module has a Scope parameter that allows you to specify whether the module is installed for the current user or for all users. For more information, see Install-Module.
- On Windows, the location of the user-specific CurrentUser scope is the $HOME\Documents\PowerShell\Modules folder. The location of the AllUsers scope is $env:ProgramFiles\PowerShell\Modules .
- On non-Windows systems, the location of the user-specific CurrentUser scope is the $HOME/.local/share/powershell/Modules folder. The location of the AllUsers scope is /usr/local/share/powershell/Modules .
In addition, setup programs that install modules in other directories, such as the Program Files directory, can append their locations to the value of $env:PSModulePath .
For more information, see about_PSModulePath.
Managing environment variables
PowerShell provides several different methods for managing environment variables.
- The Environment provider drive
- The Item cmdlets
- The .NET System.Environment class
- On Windows, the System Control Panel
Using the Environment provider
Each environment variable is represented by an instance of the System.Collections.DictionaryEntry class. In each DictionaryEntry object, the name of the environment variable is the dictionary key. The value of the variable is the dictionary value.
To display the properties and methods of the object that represents an environment variable in PowerShell, use the Get-Member cmdlet. For example, to display the methods and properties of all the objects in the Env: drive, type:
The PowerShell Environment provider lets you access environment variables in a PowerShell drive (the Env: drive). This drive looks much like a file system drive. To go to the Env: drive, type:
Use the Content cmdlets to get or set the values of an environment variable.
You can view the environment variables in the Env: drive from any other PowerShell drive, and you can go into the Env: drive to view and change the environment variables.
Using Item cmdlets
When you refer to an environment variable, type the Env: drive name followed by the name of the variable. For example, to display the value of the COMPUTERNAME environment variable, type:
To display the values of all the environment variables, type:
Because environment variables do not have child items, the output of Get-Item and Get-ChildItem is the same.
By default, PowerShell displays the environment variables in the order in which it retrieves them. To sort the list of environment variables by variable name, pipe the output of a Get-ChildItem command to the Sort-Object cmdlet. For example, from any PowerShell drive, type:
You can also go into the Env: drive by using the Set-Location cmdlet:
When you are in the Env: drive, you can omit the Env: drive name from the path. For example, to display all the environment variables, type:
To display the value of the COMPUTERNAME variable from within the Env: drive, type:
Saving changes to environment variables
To make a persistent change to an environment variable on Windows, use the System Control Panel. Select Advanced System Settings. On the Advanced tab, click Environment Variable. . You can add or edit existing environment variables in the User and System (Machine) scopes. Windows writes these values to the Registry so that they persist across sessions and system restarts.
Alternately, you can add or change environment variables in your PowerShell profile. This method works for any version of PowerShell on any supported platform.
Using System.Environment methods
The System.Environment class provides GetEnvironmentVariable and SetEnvironmentVariable methods that allow you to specify the scope of the variable.
The following example uses the GetEnvironmentVariable method to get the machine setting of PSModulePath and the SetEnvironmentVariable method to add the C:\Program Files\Fabrikam\Modules path to the value.
Win32_Environment class
The Win32_Environment WMI class represents an environment or system environment setting on a Windows computer system. Querying this class returns environment variables found in:
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Sessionmanager\Environment
HKEY_USERS\ \Environment
The following syntax is simplified from Managed Object Format (MOF) code and includes all of the inherited properties. Properties are listed in alphabetic order, not MOF order.
Syntax
Members
The Win32_Environment class has these types of members:
Properties
The Win32_Environment class has these properties.
Caption
Data type: string
Access type: Read-only
A short textual description of the object.
Description
Data type: string
Access type: Read-only
A textual description of the object.
InstallDate
Data type: datetime
Access type: Read-only
Indicates when the object was installed. Lack of a value does not indicate that the object is not installed.
Name
Data type: string
Access type: Read/write
Character string that specifies the name of a Windows-based environment variable. By specifying the name of a variable that does not yet exist, an application creates a new environment variable.
Status
Data type: string
Access type: Read-only
String that indicates the current status of the object. Operational and non-operational status can be defined. Operational status can include «OK», «Degraded», and «Pred Fail». «Pred Fail» indicates that an element is functioning properly, but is predicting a failure (for example, a SMART-enabled hard disk drive).
Non-operational status can include «Error», «Starting», «Stopping», and «Service». «Service» can apply during disk mirror-resilvering, reloading a user permissions list, or other administrative work. Not all such work is online, but the managed element is neither «OK» nor in one of the other states.
Values include the following:
OK («OK»)
Error («Error»)
Degraded («Degraded»)
Unknown («Unknown»)
Pred Fail («Pred Fail»)
Starting («Starting»)
Stopping («Stopping»)
Service («Service»)
Stressed («Stressed»)
NonRecover («NonRecover»)
No Contact («No Contact»)
Lost Comm («Lost Comm»)
SystemVariable
Data type: boolean
Access type: Read-only
Qualifiers: MappingStrings («Win32Registry|System\\CurrentControlSet\\Control\\Session Manager\\Environment»)
Indicates whether the variable is a system variable. A system variable is set by the operating system, and is independent from user environment settings.
UserName
Data type: string
Access type: Read-only
Name of the owner of the environment setting. It is set to for settings that are specific to the Windows-based system (as opposed to a specific user) and for default user settings.
VariableValue
Data type: string
Access type: Read/write
Qualifiers: MappingStrings («Win32Registry|System\\CurrentControlSet\\Control\\Session Manager\\Environment»)
Placeholder variable of a Windows-based environment variable. Information like the file system directory can change from computer to computer. The operating system substitutes placeholders for these.
Remarks
The Win32_Environment class is derived from CIM_SystemResource. You can use this class to find the paths of special folders such as the System folder or Program files on a remote machine. Some examples are: windir, systemroot, programfiles, and userprofile. Win32_Environment basically returns what can be found in:
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Sessionmanager\Environment
HKEY_USERS\ \Environment
The calling process that uses this class must have the SE_RESTORE_NAME privilege on the computer in which the registry resides. For example, if you enumerate this class on the local computer, the account under which your application runs must have this privilege. For more information, see Executing Privileged Operations.
Examples
The List Environment Variables on a Computer Perl sample uses WMI to return information about all the environment variables on a computer.
The following VBScript code example enumerates the environment variables on the local computer.
The following VBScript code example changes an environment variable named BUILD_TYPE to a value input by the user. The script assumes that the BUILD_TYPE variable already exists. If it does not exist then the script ends. The input value is checked: it must be either «Build1», «Build2», or «Build3», and no other value is accepted. The VBScript UCase function makes the input case-insensitive. If what is typed in is not one of the three acceptable values, the script ends.