Joined
·
412 Posts
[CMD, WMIC, HKLM, HKCU, SETX, PATH, 2001]
the problem is probably more than 20 years old:
- setx Path "%Path%;NewPath" pollutes the user path with the system path.
- setx Path "%Path%;NewPath" /M pollutes the system path with the user path.
- the pollution: 2022-03-14_09-02-33-CMD-PATH-x264-faststart
I am not sure if there is a safe native solution that contemplate NON-ANSI directories making exclusive use of the command prompt.
this is my attempt!
- using Microsoft Windows [Version 10.0.19042.868]
0 - check the paths
tip: wmic ENVIRONMENT get * [to see more]
info: a registry backup was performed using the above commands. the files have been saved in the current directory.
1 - backup user path at %USERPATH%
- the commands pre-suppose
info: there is some pollution* at the end of the string.
- add '/' to better see spaces
info: Naming Files, Paths, and Namespaces - Win32 apps
- remove up to 15 end spaces, then remove '/'
info: the number of spaces introduced was NOT consistent in my tests.
* when dealing with invisible characters it is easier to identify them by converting them to hexadecimal, do:
tip: use 4, 5, 10, 11 (same as none) in encodehex
see: CryptBinaryToStringA function (wincrypt.h) - Win32 apps
2 - set¹²³ user path as %USERPATH% only
done!
¹ info: setx PATH "%USERPATH%" sets the variable value instead of the literal %USERPATH%
² info: using the literal is better, because the behavior becomes dynamic instead of static. i.e., by using the literal instead of the value, only the %USERPATH% variable needs to be changed in the future. whereas, by using the value (i.e., the addresses present inside the variable), the %PATH% user variable must also be updated upon change in %USERPATH% [which is not desirable].
³ info: below methods NOT recommended! it works, but it is easier to make mistakes.
3 - for now on just use %USERPATH%
tip: close all programs, especially terminals, before continuing.
4 - re-check
5 - visual check
info: the system PATH can only be changed if the previous command was launched with administrative privilege;
6 - good sources
"Microsoft - Windows Commands"
Windows commands
"SS64 - Windows CMD Shell How-to guides and examples"
Windows CMD Command Syntax - SS64.com
DosTips - The DOS Batch Guide
DosTips - The DOS Batch Guide
"Rob van der Woude's Scripting Page"
Rob van der Woude's Scripting Pages
"TLDP - Appendix N. Converting DOS Batch Files to Shell Scripts"
https://tldp.org/LDP/abs/html/dosbatch.html
"Stack Overflow - cmd"
https://stackoverflow.com/questions/tagged/cmd
"Super User - batch"
https://superuser.com/questions/tagged/batch
Dave Benham - dbenham
Jan Erik - jeb
7 - official sound tracks
Arnaldo Baptista - Cê Tá Pensando Que Eu Sou Loki? (1974)
Ivan Lins - Dinorah, Dinorah (1977)
Bruce Springsteen - Adam Raised a Cain (1978)
8 - TL;DR: do it! / or don't!
cheers!
the problem is probably more than 20 years old:
- setx Path "%Path%;NewPath" pollutes the user path with the system path.
- setx Path "%Path%;NewPath" /M pollutes the system path with the user path.
- the pollution: 2022-03-14_09-02-33-CMD-PATH-x264-faststart
I am not sure if there is a safe native solution that contemplate NON-ANSI directories making exclusive use of the command prompt.
this is my attempt!
- using Microsoft Windows [Version 10.0.19042.868]
0 - check the paths
Code:
echo %Path%
reg query "HKCU\Environment" /v Path
reg query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v Path
reg EXPORT "HKCU\Environment" UserEnvironment.reg /y
reg EXPORT "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" SystemEnvironment.reg /y
wmic ENVIRONMENT where "username='<SYSTEM>' AND name='Path'" get variablevalue
wmic ENVIRONMENT where "username='%COMPUTERNAME%\\%USERNAME%' AND name='Path'" get variablevalue
info: a registry backup was performed using the above commands. the files have been saved in the current directory.
1 - backup user path at %USERPATH%
- the commands pre-suppose
- - non-null path;
- - functional path;
Code:
wmic ENVIRONMENT where "username='%COMPUTERNAME%\\%USERNAME%' AND name='Path'" get variablevalue | more +1 > %temp%\userpathvalue.txt
Code:
set /P USERPATH=<%temp%\userpathvalue.txt
Code:
set "USERPATH=%USERPATH%/"
- remove up to 15 end spaces, then remove '/'
Code:
set "USERPATH=%USERPATH: /=/%"
set "USERPATH=%USERPATH: /=/%"
set "USERPATH=%USERPATH: /=/%"
set "USERPATH=%USERPATH: /=/%"
set "USERPATH=%USERPATH:/=%"
Code:
echo %USERPATH%[checking final string]
setx USERPATH "%USERPATH%"
Code:
wmic ENVIRONMENT where "username='%COMPUTERNAME%\\%USERNAME%' AND name='Path'" get variablevalue | more +1 > %tmp%\0 && set /P TEMPREADER=<%tmp%\0
echo %TEMPREADER% >%tmp%\1 && echo %TEMPREADER%/ && certutil -f -encodehex "%tmp%\1" "%tmp%\2" 11 | more +4 && type "%tmp%\2"
REM to open on notepad, type: certutil -f -encodehex "%tmp%\1" "%tmp%\2" 5 && cmd /C START "" /MAX notepad %tmp%\2
see: CryptBinaryToStringA function (wincrypt.h) - Win32 apps
2 - set¹²³ user path as %USERPATH% only
Code:
setx PATH ^%USERPATH^%
¹ info: setx PATH "%USERPATH%" sets the variable value instead of the literal %USERPATH%
² info: using the literal is better, because the behavior becomes dynamic instead of static. i.e., by using the literal instead of the value, only the %USERPATH% variable needs to be changed in the future. whereas, by using the value (i.e., the addresses present inside the variable), the %PATH% user variable must also be updated upon change in %USERPATH% [which is not desirable].
³ info: below methods NOT recommended! it works, but it is easier to make mistakes.
Code:
reg add "HKEY_CURRENT_USER\Environment" /v PATH /d ^%USERPATH^% /f
wmic ENVIRONMENT set name="PATH", variablevalue=^%USERPATH^%, username="%COMPUTERNAME%\\%USERNAME%"
Code:
setx USERPATH "%USERPATH%;ENTER-NEW-PATH-HERE"
4 - re-check
Code:
echo %Path%
reg query "HKCU\Environment" /v Path
reg query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v Path
wmic ENVIRONMENT where "username='<SYSTEM>' AND name='path'" get variablevalue
wmic ENVIRONMENT where "username='%COMPUTERNAME%\\%USERNAME%' AND name='path'" get variablevalue
Code:
rundll32 sysdm.cpl,EditEnvironmentVariables
6 - good sources
"Microsoft - Windows Commands"
Windows commands
"SS64 - Windows CMD Shell How-to guides and examples"
Windows CMD Command Syntax - SS64.com
DosTips - The DOS Batch Guide
DosTips - The DOS Batch Guide
"Rob van der Woude's Scripting Page"
Rob van der Woude's Scripting Pages
"TLDP - Appendix N. Converting DOS Batch Files to Shell Scripts"
https://tldp.org/LDP/abs/html/dosbatch.html
"Stack Overflow - cmd"
https://stackoverflow.com/questions/tagged/cmd
"Super User - batch"
https://superuser.com/questions/tagged/batch
Dave Benham - dbenham
Jan Erik - jeb
7 - official sound tracks
Arnaldo Baptista - Cê Tá Pensando Que Eu Sou Loki? (1974)
Ivan Lins - Dinorah, Dinorah (1977)
Bruce Springsteen - Adam Raised a Cain (1978)
8 - TL;DR: do it! / or don't!
Code:
REM the commands pre-suppose
REM - non-null path;
REM - functional path;
REM if null or unfunctionl path don't progress!
reg query "HKCU\Environment" /v Path
reg query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v Path
reg EXPORT "HKCU\Environment" UserEnvironment.reg /y
reg EXPORT "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" SystemEnvironment.reg /y
wmic ENVIRONMENT where "username='%COMPUTERNAME%\\%USERNAME%' AND name='Path'" get variablevalue | more +1 > %temp%\userpathvalue.txt
set /P USERPATH=<%temp%\userpathvalue.txt
set "USERPATH=%USERPATH%/"
set "USERPATH=%USERPATH: /=/%"
set "USERPATH=%USERPATH: /=/%"
set "USERPATH=%USERPATH: /=/%"
set "USERPATH=%USERPATH: /=/%"
set "USERPATH=%USERPATH:/=%"
echo %USERPATH%[checking final string] && echo %USERPATH% > %tmp%\0
certutil -f -encodehex "%tmp%\0" "%tmp%\1" 11 | more +4 && type "%tmp%\1"
setx USERPATH "%USERPATH%"
setx PATH ^%USERPATH^%
echo done!
REM setx USERPATH "%USERPATH%;ENTER-NEW-PATH-HERE"
REM please close all programs