Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The CD command returns the current directory, and the %CD% variable returns the same result in an environment variable:


c:\Temp\test\test1>@echo %cd%

This command echoes the result of the CD command

for /F %f in ('cd') do @echo %f

so I should get


but actually I get:

c:\Temp\test\test1>for /F %f in ('cd') do @echo %f

but using %CD% gives me this:

c:\Temp\test\test1>for /F %f in ('@echo %cd%') do @echo %f

This was working on my machine when I last tested it a few months ago (it's in not often used script).

I've tested it on another machine and it works fine there. So I'm thinking that this is due to how I open the command prompt and set the working directory to c:\work\Consulting using the following registry script:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
"Autorun"="cd /d C:\\Work\\Consulting"

(FYI this is needed if you want open an elevated command prompt to a particular directory)

Alas no!

Edit: Of course I have a workaround, but I was wondering what it is that I've done to cause this happen.

share|improve this question

Yes, your Autorun registry entry is causing the problem. FOR /F executes commands within the IN() clause via CMD.EXE. And CMD.EXE runs the Autorun unless the /D option is used. But there is no way to force FOR /F to use the /D option. :(

I also like to have my command shell open in a particular folder, but I don't use Autorun. Instead I create a shortcut and edit the properties to have "Start in:" set to my desired folder.

If you really want, I think you can still use Autorun. I believe you really only want your Autorun to CD upon the initial instantiation of your CMD session. You could modify your registry entry as follows to achieve that:

[HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
"Autorun"="if not defined AutoRunComplete set AutoRunComplete=TRUE&cd /d C:\\Work\\Consulting"

If your Autorun logic gets more complex, you might want to create a batch script and then have Autorun call that script.

share|improve this answer
Thanks mate, that's excellent reasoning. The reason I'm using AutoRun is that setting the directory only works in the way you say for non Elevated Command prompts. If you try to an admin prompt it defaults to c:\windows\system32 unfortunately. Your Workaround is the best of all worlds thank you! – Preet Sangha Dec 31 '12 at 0:46
"FOR /F executes commands within the IN() clause via CMD.EXE" Oh god! guy you saved me from insanity ^^ – Gab Jan 13 '15 at 12:30

Why don't you disable HKEY_CURRENT_USER\Software\Microsoft\Command Processor\Autorun altogether and instead create a shortcut like the following?


share|improve this answer
But will it work if the script calls START? But for the problem I've outlined that's a nice work around thanks. – Preet Sangha Jan 1 '13 at 1:09
"if the script calls start" - Meaning? You want to open an elevated command prompt at a specific directory via a script or batch file? – Karan Jan 1 '13 at 1:14
no was just thinking out loud. – Preet Sangha Jan 1 '13 at 3:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .