Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


.bat shutdown script

Posted on 2014-02-14
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
LVL 41

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 41

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.
Simplify Your Workload with One Tool

How do you combat today’s intelligent hacker while managing multiple domains and platforms? By simplifying your workload with one tool. With Lunarpages hosting through Plesk Onyx, you can:

Automate SSL generation and installation with two clicks
Experience total server control

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?
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 41

Accepted Solution

Kyle Abrahams earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article is a collection of issues that people face from time to time and possible solutions to those issues. I hope you enjoy reading it.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
Simple Linear Regression
Six Sigma Control Plans

636 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