Repeatedly execute a Windows command from a batch file?

16,482

I agree with commenter @reuben, that you possible should look for other options (including getting that coworker himself "fixed"), but anyway here is a way to run the command repeatedly:

:loop
shutdown -a
goto loop

Note that this might still leave a window, where it cannot cancel a shutdown because it is not "fast" enough.

Also it causes "mild terror" to your system, at least on the csrss.exe (or conhost.exe) process, because the console window will be busy printing messages. Thus you might want to redirect the output at least (shutdown -a > NUL 2>&1) and/or introduce some delay beteween the calls to shutdown. Of course the later increases the risk of not "catching" a shutdown significantly.

All in all that is not a good solution to your problem. Sorry.

Share:
16,482
RandomPhobia
Author by

RandomPhobia

Updated on June 14, 2022

Comments

  • RandomPhobia
    RandomPhobia almost 2 years

    I'm not too familiar with batch files for Windows, so this may seem like a beginner question. How could I "loop", or repeat, a command like the following?

    shutdown -a
    
  • reuben
    reuben almost 12 years
    Are you assuming that the OP has ssh, bash, and other Unix tools installed on the Windows machines in question?
  • RandomPhobia
    RandomPhobia almost 12 years
    @LMC - No 'Shutdown -a' at least on windows is a command to abort shutdown
  • RandomPhobia
    RandomPhobia almost 12 years
    @Reuben - Unless Windows has them pre-installed they probably wouldn't be on the computer and I think I would get in trouble for adding things
  • LMC
    LMC almost 12 years
    You are entirely correct. I don't know how I could have completed missed the word "Windows" in the question. Sorry about that.