PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, August 20, 2022

[FIXED] Why is setx path not working?

 August 20, 2022     environment-variables, path, windows     No comments   

Issue

Can someone explain this result?

After setting path, it did not change. This was run in an Administrator command-line:

C:\Windows\system32>setx path "C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\ProgramData\chocolatey\bin;D:\Program Files (x86)\Microsoft VS Code\bin"

SUCCESS: Specified value was saved.

C:\Windows\system32>path
PATH=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\ProgramData\chocolatey\bin;D:\Program Files (x86)\Microsoft VS Code\bin;D:\Program Files (x86)\metapad36;D:\Program Files (x86)\metapad36" /M

I've read that %PATH% = PATH variable for machine + PATH variable for user. Am I seeing the machine path + Admin's path?

Have looked at other articles on the topic, but still confused.

Should I clear the user paths, so there's no duplication?

update: Re the tip that "variables created or modified by this tool will be available in future command windows" i open a non-admin window and enter:

>path
PATH=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\ProgramData\chocolatey\bin;D:\Program Files (x86)\Microsoft VS Code\bin;;D:\Program Files (x86)\metapad36;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\ProgramData\chocolatey\bin;D:\Program Files (x86)\Microsoft VS Code\bin

The path is repeated twice. Ok, then at same prompt I setx the path without the repeat, and without /M:

>setx path "C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\ProgramData\chocolatey\bin;D:\Program Files (x86)\Microsoft VS Code\bin"

SUCCESS: Specified value was saved.

Apparently saved in current user environment.

Then i open a new non-admin command window, and:

>path
PATH=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\ProgramData\chocolatey\bin;D:\Program Files (x86)\Microsoft VS Code\bin;;D:\Program Files (x86)\metapad36;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\ProgramData\chocolatey\bin;D:\Program Files (x86)\Microsoft VS Code\bin`

It has not changed. Why?


Solution

In Windows, each process gets a copy of the environment which is essentially a snapshot of the global environment at the time the process was started. Changes to the global environment while the process is running are not propagated back to the process' own copy of the environment.

To answer the actual question, setx does modify the user environment (or the system one if run with /M), but the changes are not immediately visible in the process that executed setx, in this case cmd.exe. If you open a new command prompt after running setx, you'll see the changes in that instance of cmd.exe.

This is explicitly noted in the setx /? help:

On a local system, variables created or modified by this tool will be available in future command windows but not in the current CMD.exe command window.

To effect the same changes in both the global environment, and the one of the current process, you need to run both setx andset.



Answered By - dxiv
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing