0

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.).

  • Two minutes on google: technet.microsoft.com/en-us/library/ff730964.aspx – Ouroborus Aug 11 '16 at 7:08
  • @Ouroborus I've found this page but: 1. there is not explicitly shown differences between user and system environment variables 2. it is not shown how to append (or concatenate) variables as I've did in %PATH% example. I'm looking for complete solution that works because I'm not PowerShell guru (I'm from Unix world). – Wakan Tanka Aug 11 '16 at 7:12
3

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.)

  • How to drop the path form environment variable after using your methed "[Environment]::SetEnvironmen..." to create. – yode Jul 13 '17 at 18:56
  • @yode In my first example, the original PATH is first stored in $path. In that case, reverting to the original PATH would be something like [Environment]::SetEnvironmentVariable("PATH",$path,"User"). – Ouroborus Jul 14 '17 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.