I have a batch file that I'd like to be able to be run by double-clicking on the file in Windows Explorer. When this is done, I want to end with a PAUSE so the window doesn't immediately close.

But if the batch file is run from a command shell, I'd prefer to not end with a PAUSE.

Is there some way to tell, within a batch file, whether it is running in a command-line spawned from Windows Explorer, or from an existing command shell?

Bash provides the special $- environment variable.

Is there something similar in cmd.exe?


Not an exact solution, but you can create a shortcut to your cmd file and add a command-line parameter to the target. When you need to run your cmd from Explorer, you will have to start she shortcut, not the cmd file. In your cmd file you will test %1 parameter to identify whether it started from shortcut (from explorer) or from command prompt.

  • That's far from optimal, but if MS doesn't make the info available, it may be what we have to go with. – Jeff Dege Mar 7 '18 at 22:17

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.