Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Whenever I change environment variable(in Windows 7) I have to exit and start the command prompt. This is necessary or the changes are not reflected.
Is there any command (or simple method) to restart the command prompt at the same location ?

Update: techie007's original solution start cmd && exit did answered what my question literally meant. But it did not solve my problem. Looks like there is no simple method. I tried the scripts given at Stack Overflow. The scripts worked for me so I mark techie007's answer as accepted.

share|improve this question
    
you can always just go to RUN and type CMD but im not sure thats exactly what you want –  That Guy Feb 10 '12 at 21:22
    
@ThatGuy That will do but the new prompt will not open at the same location. –  Serious Feb 12 '12 at 15:18

3 Answers 3

up vote 2 down vote accepted

C:\> start cmd should do the trick to open a new command prompt window in the current folder.

You could also use C:\> start cmd && exit to open a new CMD prompt and then close the window you launched it from, if it managed to open it successfully.

Use just one & to have it close the launching window regardless of success.

edit: Now that grawity was nice enough to point out that I didn't comprehend... :)

There's (intentionally) no inherent way to do this in Windows.

This question was also asked on Stack Overflow and there's some good explanations and a possible script-based work-around there:

Is there a command to refresh environment variables from the command prompt in Windows?

share|improve this answer
    
This will not work the way Serious wants, since the new shell will inherit old environment variables from its parent. –  grawity Feb 10 '12 at 14:45
    
@grawity Ooooh... duhh.. I was thinking he wanted to carry over existing (local) variables.. –  Ƭᴇcʜιᴇ007 Feb 10 '12 at 14:56
    
@grawity This is what's happening. Even if I hold shiht, right click and start CMD prompt (?_?). But when I start CMD prompt from start menu it is aware of new variales. Thanks techie007. I did not know we can use AND or short-circuit AND on CMD prompt. It deserves an upvote. –  Serious Feb 10 '12 at 14:57
1  
& is not "AND", it is a simple command separator. (&& is short-circuit AND, || is OR.) –  grawity Feb 10 '12 at 15:02

Try this script: update-env.cmd

Edit: Inline script removed due to tab→space mangling by SuperUser. Use the "Raw" link in the above page to download.

This works since, when ran interactively, batch scripts run in the parent context for historical reasons, and have the ability to update the interactive shell's environment.

share|improve this answer
    
what's __ mean? "REALLY local variable"? ;D –  Ƭᴇcʜιᴇ007 Feb 10 '12 at 15:10
    
@techie007: Since the script is running in the parent context (without a setlocal), I had to choose variable names that would be really unlikely to already exist, while remaining human-understandable. (By convention, variables, functions, methods and other constructs whose names start with a _ are considered to be "internal", "magic", or "special"; the more _'s, the more magic. I decided that maybe two _'s will provide enough magic to hold the script in a single piece without having to resort to duct tape and chewing gum.) –  grawity Feb 10 '12 at 15:15
    
I was at G:\test>. I added D:\ to PATH variable. Then I ran update-env.cmd from this prompt(after copying it to test). Tried to run p1.exe(in D:\ drive) as G:\test>p1. But no success. Am I doing something wrong ? –  Serious Feb 10 '12 at 15:28
    
@Serious: After update-env, what does set path output? Does it show anything sensible? Does it include `D:` in the output? –  grawity Feb 10 '12 at 15:34
    
No. It shows old path i.imgur.com/gyHqt.png –  Serious Feb 10 '12 at 15:40

You're not actually setting any environment variables of any form when you modify the registry via My Computer.

Environment variables are not kept in the registry. What is kept in the registry is a template, from which programs like Windows Explorer (re-)construct their environment variables when notified to do so. Actual environment variables are per-process and are stored in each process' own address space, initially inherited from its parent process and modifiable thereafter at the process' whim. The programs like Windows Explorer participate in a voluntary protocol, whereby they will re-read the template, and update their own per-process environment whenever a Windows message is broadcast to all (top-level) windows on a desktop.

Many Win32 programs do not participate in this voluntary protocol. Microsoft's command interpreter is one such program. To modify an environment variable within a running Microsoft command interpreter process, one uses the ordinary command interpreter commands for modifying environment variables, such as SET, DPATH, and PATH. The modified environment will be inherited by every process that the command interpreter spawns.

Similarly, the modified environment in a Windows Explorer process is only inherited by processes that are spawned by Windows Explorer after it has received the message and re-read the template. The environment variables of an already running process are not affected by modifications made by other processes to their own environment variables. An already running Microsoft command interpreter won't magically gain a changed environment from the Windows Explorer process that spawned it before the change. Furthermore, grandchildren of Windows Explorer only inherit from their parent, the command interpreter, and so don't inherit any environment changes that Windows Explorer has made to its own environment.

Other command interpreters differ. JP Software's TCC, for example, itself participates in the voluntary protocol. It will recognize the Windows message, and update its own per-process environment from the template in the registry, when its "Update Environment on System Change" setting is enabled.

share|improve this answer
    
Basically, as far as I understand it, the registry is virtualised in Vista onwards, and it is not the real copy that is altered. This explanation does not seem to contradict what you said. –  paradroid Feb 10 '12 at 21:23
    
I always thought all that TCC setting did was update Explorer windows without having to refresh. Now I know it is much more useful than that. –  paradroid Feb 10 '12 at 21:25
    
So that's why grawity's method is not working. –  Serious Feb 12 '12 at 15:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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