1

Can anyone explain when/why it is necessary to restart Powershell and/or cmd prompt (if they behave differently with respect to this question)?

For example, in order to work with the most up-to-date machine state, must I restart Powershell after:

1) Editing system environment variable such as Path

2) Installing things such as npm modules

3) Editing a file such as a .txt or a file within a VSCode project

Thanks in advance!

2

Generally you only need reload your shell when your environment changes - its generally quick and easy and basically guaranteed to work. But it can be annoying, and depending on what your doing not strictly necessary.

Both CMD and Powershell read the user/system environment variables at startup and apply them to the current shell. When you make changes to values like PATH from within this shell, your just updating the default values used by any new shells (CMD or POSH), and generally not changing the current shells environment.

Things like the PATH can be changed within the current shell, but its done differently in CMD vs POSH. Here is a simple demo that shows this in action with command prompt.

Open a CMD prompt, and run these to commands to setup the demo:

C:\>mkdir c:\pathtest
C:\>echo @echo hello > c:\pathtest\sayhello.cmd

if you switch to c:\pathtest and execute sayhello, it should work as expected and print "hello" to the screen:

C:\>cd c:\pathtest
C:\pathtest>sayhello
hello

Now, change directory back to the root, and run sayhello again and you get a command not recognised error:

C:\pathtest>cd ..
C:\>sayhello
'sayhello' is not recognized as an internal or external command,
operable program or batch file.

Now the magic, the below updates the PATH of your current shell(only) to include c:\pathtest, and allows your to run sayhello from anywhere.

C:\>SET PATH=%PATH%;c:\pathtest
C:\>sayhello
hello

Close the CMD prompt, and open a new one, and the PATH statement has reverted to its original value and sayhello will no longer work. (you can delete c:\pathtest now too).

The same applies to Powershell - you can manipulate the builtin $env variable to alter the PATH etc for just the current session.

So, back to your original questions:

For example, in order to work with the most up-to-date machine state, must I restart Powershell after:

1) Editing system environment variable such as Path

2) Installing things such as npm modules

3) Editing a file such as a .txt or a file within a VSCode project

  1. If your environment variables change (like installing something that modifies the PATH) restating your shell is the easiest fix - but as described above, you can work around this if you know what change was made and manually apply it to your current shell.

  2. npm modules sit on the filesystem so unless they are modifying the PATH or such you shouldnt need to reload the shell to see changes.

  3. As above, when you hit save in your editor, that data is written to disk. When you execute POSH/CMD its read from the disk by the interpreter at runtime.

1

A process that listens for the WM_SETTINGCHANGE window message will be capable of updating its current environment variables without restarting.

I think ignoring WM_SETTINGCHANGE from Powershell and CMD is intentional to ensure that environment variables can't be changed during the execution of a script.

More info:
Environment Variables
Restarting Windows to refresh Local System environment variables

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.