Windows – Manipulation with user and system environment variables from command line in Windows 10

environment-variablespowershellwindows 10

I've been using following commands to set/remove user/system environment variables:

SYSTEM:

setx -m PATH "%PATH%C:\Program Files\Git\cmd;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\bin;"

USER:

setx VAR1 "CONTENT OF VAR1"

Later I've found those limitations:

  1. If the %PATH% is too long then I get following warning and the %PATH% variable is truncated:

WARNING: The data being saved is truncated to 1024 characters.

  1. As the help for setx command says:

When you use Setx.exe to clear an environment variable value, the
environment variable name is not affected

In other words when I run setx -m OCVLIBDIR "" then the OCVLIBDIR will not be deleted but rather empty.

How can I overcome those limitations?

PS: I prefer tools that are part of Windows 10 (e.g. PowerShell) but it is not problem to use also 3rd party tools (although it seems to me like overkill).

PPS: I know it is possible to edit variables via windows GUI, registers or some 3rd party GUI tools but I prefer scripts because I can have scripted multiple scenarios and fast switch between them.This is useful when I'm experimenting with various libraries on my system (e.g. various versions of opencv etc.).

Best Answer

PowerShell

This TechNet article describes the basics.

This answer describes getting system vs user environment variables (which was only hinted at in the first article). "Machine" for system, "User" for the current user, and "Process" for the current process which works for getting the final system/user combo.

PowerShell concatenation is talked about in this question and its answers. You can assemble strings using + to concatenate or use substitution as in "{0} {1}" -f "Hello","World".

Parenthesis are used to group constructs together.

Variables are un-typed, undeclared, and start with $.

Putting it all together gets you something like:

$path = [Environment]::GetEnvironmentVariable("PATH","User")
[Environment]::SetEnvironmentVariable("PATH",$path+";C:\Temp","User")

Or as a one-liner:

[Environment]::SetEnvironmentVariable("PATH",([Environment]::GetEnvironmentVariable("PATH","User"))+";C:\Temp","User")

This article describes how to delete environment variables ($null is a special variable):

[Environment]::SetEnvironmentVariable("MyTestVariable",$null,"User")

Changes to the environment variables done this way are globally available.


CMD

reg.exe can add, change, and delete registry entries. Environment variables live in the registry. This answer talks about using reg to delete environment variables.

For user environment variables:

reg DELETE HKCU\Environment /v envvarname

For system environment variables:

reg DELETE "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v envvarname

(HKCU is interchangeable with HKEY_CURRENT_USER. HKLM is interchangeable with HKEY_LOCAL_MACHINE.)

(Note that CMD treats empty environment variables the same as non-existent environment variables. I understand, though, that you're trying to purge the variable from the system.)