.bat shutdown script

Posted on 2014-02-14
Last Modified: 2014-04-02
I have a .bat script running on a server as a scheduled task to shutdown 2 workstations on the network every night.

the script is this:

@echo off
set list="c:\scripts\shutdowns.txt"
set log="c:\scripts\shutdownlog.txt"
for /f %%a in ('type %list%') do (
  shutdown -s -f -t 60 /m \\%%a
  ECHO %date%,%time%,%%a,%Errorleverl%
)> %log%

it sources the computer names on shutdowns.txt:


and produces a log file shutdownlog.txt:

Thu 02/13/2014,20:30:00.70,aa2-pc,

the problems with the script are:
1) log file only shows last PC on shutdowns.txt with no error message
2) both workstations did not shut down, probably due to the fact that they were in sleep mode.  this wasnt reflected in the log file as well.

If you could help me fix this that would be greatly appreciated!

Question by:tike55
  • 3
  • 3
  • 2
  • +2
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 39860434
> %log%
should be

>> %log%

that will append rather than overwrite.
LVL 20

Expert Comment

ID: 39860439
I run a VBS script that seems to work very well. You can use it to shut down all your network if you want it uses the "servershutdown.txt" file in the script with the names or IP's of the computers you want shutdown.

You might try it I might work better for you.

Set WSHShell = WScript.CreateObject("WScript.Shell")
 Set oFS = CreateObject("Scripting.FileSystemObject")

 strWarning = "Due to network maintenance, this computer must be shutdown. You have 5 minutes to save your work from the start of this countdown. Sorry for any inconvenience caused. "
 strDelay = 300 'Delay given in seconds; change this value to your preference, or set it to 0 to give no delay at all

 'Open a text file of computer names
 'with one computer name per line
 Set oTS = oFS.OpenTextFile("C:\Scripts\servershutdown.txt")

 'go through the text file
 Do Until oTS.AtEndOfStream
 'get the next computer name
 'store it in variable strCompname
 strCompname = oTS.ReadLine
 WshShell.Run "C:\Windows\System32\shutdown.exe -m \\" & strCompname & " -s -f -c " & Chr(34) & strWarning & Chr(34) & " -t " & strDelay 'Replace the "-r" switch with "-s" to make the computers shutdown instead
 'close the text file

EDIT: Sorry that didn't answer your question at all after re-reading. My apologies.
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 39860460
Also from here:

you may need to authenticate yourself first.  (via the map).  They also have a power tools shutdown link available.
LVL 43

Expert Comment

by:Steve Knight
ID: 39860552
Actually it was intended to be this, the extra ( ) around the for command redirects the output in one go to log file.... for this many doesn't make much difference to using >> mind!

@echo off
set list="c:\scripts\shutdowns.txt"
set log="c:\scripts\shutdownlog.txt"
(for /f %%a in ('type %list%') do (
  shutdown -s -f -t 60 /m \\%%a
  ECHO %date%,%time%,%%a,%Errorleverl%
))> %log%

Author Comment

ID: 39860629
Hi Dragon-it since this is your script I'll ask ya,

1)  how can I get it to shutdown the PCs when they are in sleep mode?  PCs were on in the morning.

2)  also what does the 60 /m refer to?
New My Cloud Pro Series - organize everything!

With space to keep virtually everything, the My Cloud Pro Series offers your team the network storage to edit, save and share production files from anywhere with an internet connection. Compatible with both Mac and PC, you're able to protect your content regardless of OS.

LVL 43

Expert Comment

by:Steve Knight
ID: 39860738
1)  No idea sorry, will have to unsleep it first, not sure if that is possible from network - after all it won't be responding.  It may be possible to use something that will send a WOL (wake on lan) packet to the machine which would then need to be configred for WOL in BIOS or whatever that machine needs.  It would then have to boot, to then respond to shutdown command..  

2) the 60 goes with the -t , it means give it 60 seconds before starting shutdown. The -m is the option to tell it which computer.  Shutdown -? from a cmd.exe prompt will show you the options.

Maybe better, disabled "sleep" and set a scheduled job on the PC to run itself a shutdown command?


Author Comment

ID: 39860752
Last question then this is put to bed.  Do you think it will lower the lifespan of the HDs if I  disable sleep?

BTW, I will be adding alot of workstations to this script.
LVL 43

Expert Comment

by:Steve Knight
ID: 39861041
I wouldn't have thought it would make much difference, though the amount of electricity used will be more etc... what is the idea here, the machines stay on, you run stuff in the evening then shut them down?

Expert Comment

ID: 39861321
Hmm, If energy consumption and workstation longevity is your goal (and not eg. enforcing no usage) IMO You should consider setting a scheduled task for every workstation powering off after say 20:00 if idle.

This allows for more scenario flexibility (eg: one important user needs to work late just at one special time.)

Here's a suggestion: if idle after XX:YY poweroff, recheck every 30mins,

I don't remember at the top of my head how to push/export/manage scheduling events to all clients. Should be a well-lit road though.

(If not, last time I checked, all task resides as files in a directory, making it possible to create the task once and then copy it to that dir on all workstations. Ugly hack you shouldn't have to resort to though. Needs a bit of testing.)

for whats it worth,
LVL 40

Accepted Solution

Kyle Abrahams earned 500 total points
ID: 39866019
Just a note you can configure the power options to turn of the HDs while avoiding sleep.  (EG: turn off HDs after 30 minutes) or something.  This will keep the computers running while not allowing them to go to sleep and thereby be accessible.  As a side note I rarely shut down my home PC and have had 0 HD problems with it (was using a 10K RPM WD) . . . for about 9 years.

though ola_eriks link also provides some good suggestions.

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Even if you have implemented a Mobile Device Management solution company wide, it is a good idea to make sure you are taking into account all of the major risks to your electronic protected health information (ePHI).
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
After creating this article (, I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now