I have a Server 2012 R2 Standard 64 Bit virtual machine that is acting strangely. Bat files work fine and the CMD window closes normally; but if the folder containing the bat file is closed during execution of the bat file it will finish but the CMD window does not close. I have simplified this to the point of having the simplest possible bat file. It contains 1 line. pause

  1. Open the folder containing the bat file with Windows Explorer
  2. Start the bat file by double clicking on it
  3. Close the Windows Explorer window
  4. Press any key in CMD window
  5. Cursor moves to next line as bat file is finished
  6. CMD window remains

This only happens on one machine. Does anybody know why this might be? It is a test VM and maybe should just be rebuilt, but it has me curious. Thanks

I replaced my simple bat file with one containing the following:

@assoc .bat
@for /F "tokens=1,* delims==" %%G in ('assoc .bat') do @ftype %%H
@reg query "HKLM\Software\Microsoft\Command Processor" /v AutoRun 2>NUL
@reg query "HKCU\Software\Microsoft\Command Processor" /v AutoRun 2>NUL
@wmic process where "caption = 'cmd.exe'" get CommandLine, ParentProcessId,     ProcessID
@echo please close Windows Explorer before continuing
@wmic process where "caption = 'cmd.exe'" get CommandLine, ParentProcessId,     ProcessID

Results are shown below:

batfile="%1" %*

CommandLine                                      ParentProcessId  ProcessId
C:\Windows\system32\cmd.exe /c ""D:\test.bat" "  1608             8460

please close Windows Explorer before continuing
Press any key to continue . . .
CommandLine                                      ParentProcessId  ProcessId
C:\Windows\system32\cmd.exe /c ""D:\test.bat" "  1608             8460

Press any key to continue . . .


04/13/2016 I did find out a bit more by looking at Task Manager while doing this. When I start the bat file 2 lines appear in the Task Manager - Details window... 1 for cmd.exe and 1 for conhost.exe. When the bat finishes cmd.exe goes away, but conhost.exe remains. One other thing I noticed is that you don't really have to exit the Windows Explorer folder as I previously stated... all you have to do is click anywhere in the Explorer window. If I don't click anywhere in the Explorer window the CMD window for the bat file closes normally and both tasks disappear from the details window as we would expect.

  • 2
    Unfortunately I cannot help. But I have to complement you on providing a well written question that precisely describes your issue, I wish more first time posters took the time to write a well written question like this. Of course I cannot reproduce the behavior on my Win 10 machine. And since you only see the behavior on one specific machine, it may be difficult for anyone to diagnose what is going on. – dbenham Apr 9 '16 at 3:56
  • Thanks @dbenham. I respect your knowledge and the wisdom you dispense on SO under the batch-file tag. So I am a first time poster here on Super User but I know what you mean about new posters. Yes, I agree it is not likely that someone can duplicate... just hoping maybe someone else may have encountered this. It's just a test VM so it is not critical. Just would like to know what it is in case it ever happened on a customer's machine. – RGuggisberg Apr 9 '16 at 12:53
  • 1
    @RGuggisberg - Be sure you merge your accounts so you can edit your own questions. You don't have to add indications you edited your question, the system does that for you, completely unnecessary. – Ramhound Apr 11 '16 at 13:43
  • 1
    I also had the same issue on one of my VM, as a workaround I have explicitly mentioned exit command to close the CMD window. May be this might help. – manjesh23 Apr 22 '16 at 9:37
  • 1
    Have you tried the Analyze Wait Chain feature of Resource Monitor to determine on which application the ConHost.exe process is waiting? – Xtremity Apr 22 '16 at 19:38

This is unlikely but you seem to have ruled out all the likely things. Check the following two registry entries to see if they contain anything:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor


HKEY_CURRENT_USER\SOFTWARE\Microsoft\Command Processor

Anything in there will get called each time you run a batch file, and if it has cmd /k that would cause this behavior.

  • Thanks... that is not it either. It is just a test machine, so I am no longer trying to figure it out. – RGuggisberg May 5 '16 at 21:21

Have you tried this?

@echo off
rem some code

Same things, but allows same as pause and exits by itself.

Also, why are you putting the @ in front of everything? Why not just use @echo off before the rest of the commands?

  • Yes.. already tried that. Agree on the @ preface. Typically that is what I do. Does not matter for this. Probably had that in there for some initial testing to see commands. – RGuggisberg May 16 '16 at 18:42

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.