Skip to main content

Windows batch file script to shutdown/reboot computers async - from a CSV file list.

@echo off
setlocal

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Machine List Shutdown/Reboot
::
:: Remotely Restart or Shutdown multiple computers from a file/csv
::
:: For help...
:: C:\> FastShutdown.cmd /help
::
:: Gastone Canali
:: version 2.2 31/03/2016
::
:: https://gallery.technet.microsoft.com/Shutdown-Ultra-rapido-per-a26bdbfc
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: set some variables
call :_init_vars  "%*"

:: Time_Out period before shutdown T_O xxx seconds.
set /A "T_O=600"

:: T_O in Minutes, only for the message
set /A "min=%T_O% / 60"

:: Message
set MSG="Shutting down Windows automatically %min% minutes from now..."

call :_SHUTDOWN_from_list "c:\pc\listPCs.txt"

goto :_END

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: PROCEDURES
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:_init_vars
    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    :: VARIABLES
    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    echo.|set /p a="%~1"|findstr   /R /c:"/?" /c:"/help$" /C:"/h$" 1>nul 2>nul && goto :_HELP

    :: script path
    set ScrPathTmp=%~dp0

    :: Remove the last character  c:\scrip\path\  then  c:\scrip\path
    set ScrPath=%ScrPathTmp:~0,-1%

    :: set path
    set path=%ScrPath%;%ScrPath%\bin;%path%

    :: script name
    set filename=%~n0
    set logfile="%ScrPath%\log\_%filename%.txt"
    set accodaLOG=^>^> %logfile% 2^>^&1

    :: Wait time between one compute to another
    set "waittime=1"

    :: ping number
    :: If using pc name, set the value to 2
    set "pingnum=2"

    :: Time in milliseconds to wait for a reply for each packet sent.
    set "pingwait=15"

    :: make dir for log files
    if not exist "%ScrPath%\log\" mkdir "%ScrPath%\log"  >NUL  2>&1

    goto :eof

:_SHUTDOWN_from_list
    rem list %~1
    for /f "eol=;" %%c in ('type "%~1"') do (
        call :_SHUTDOWN %%c
        call :_WAIT %waittime%
    )
    goto :eof

:_SHUTDOWN_ABORT_from_list
    rem list %~1
    for /f "eol=;" %%c in ('type "%~1"') do (
        call :_SHUTDOWN_ABORT %%c
        call :_WAIT %waittime%
    )
    goto :eof

:_REBOOT_from_list
    rem list %~1
    for /f "eol=;" %%c in ('type "%~1"') do (
        call :_REBOOT %%c
        call :_WAIT %waittime%
    )
    goto :eof

:_SHUTDOWN
    title %1
    echo >%temp%\%1.cmd @echo off
    echo>>%temp%\%1.cmd title=%1
    echo>>%temp%\%1.cmd ping %1 -4 -n %pingnum% -w %pingwait%^|find  "TTL=" ^>nul ^&^& (echo alivee ^&^& goto :_FAI)
    echo>>%temp%\%1.cmd goto :_END
    echo>>%temp%\%1.cmd :_FAI
    echo>>%temp%\%1.cmd echo ALIVE  %1 - shutdown!!
    echo>>%temp%\%1.cmd title Switched On %1 in Shutdown!!
    echo>>%temp%\%1.cmd color A0
    echo>>%temp%\%1.cmd shutdown -s -f -t %T_O% -c %MSG% -m %1   ^>"%temp%\%1.log"  2^>^&1
    echo>>%temp%\%1.cmd echo %1 ALIVE, shutdown sent... at %time%  %date% ^>"%temp%\%1.log"  2^>^&1
    echo>>%temp%\%1.cmd :_END
    echo>>%temp%\%1.cmd exit
    start c:\windows\system32\cmd.exe /c "%temp%\%1.cmd"
    goto :eof

:_SHUTDOWN_ABORT
    title=%1
    echo pc interrompo lo shutdown %1   %accodaLOG%
    echo >%temp%\%1.cmd @echo off
    echo>>%temp%\%1.cmd title=%1
    echo>>%temp%\%1.cmd ping %1 -4 -n 2^|find  "TTL=" ^>nul ^&^& (echo alive ^&^& goto :_FAI)
    echo>>%temp%\%1.cmd goto :_END
    echo>>%temp%\%1.cmd :_FAI
    echo>>%temp%\%1.cmd echo ALIVE  %1 - ABORT shutdown!!
    echo>>%temp%\%1.cmd shutdown -a -m %1   ^>"%temp%\%1.log"  2^>^&1
    echo>>%temp%\%1.cmd echo %1 rem pause>>"%temp%\%1.log"
    echo>>%temp%\%1.cmd :_END
    echo>>%temp%\%1.cmd exit
    start c:\windows\system32\cmd.exe /c "%temp%\%1.cmd"
    goto :eof

:_REBOOT
    title %1
    echo >%temp%\%1.cmd @echo off
    echo>>%temp%\%1.cmd title=%1
    echo>>%temp%\%1.cmd ping %1 -4 -n %pingnum% -w %pingwait%^|find  "TTL=" ^>nul ^&^& (echo Switched On ^&^& goto :_FAI)
    echo>>%temp%\%1.cmd goto :_END
    echo>>%temp%\%1.cmd :_FAI
    echo>>%temp%\%1.cmd echo ALIVE  %1 - Ti riavvio!!
    echo>>%temp%\%1.cmd title Switched On %1 in reboot!!
    echo>>%temp%\%1.cmd color A0
    echo>>%temp%\%1.cmd shutdown -r -f -t %T_O% -c %MSG% -m %1   ^>"%temp%\%1.log"  2^>^&1
    echo>>%temp%\%1.cmd echo %1 ALIVE, Reboot sent... at %time%  %date% ^>"%temp%\%1.log"  2^>^&1
    echo>>%temp%\%1.cmd :_END
    echo>>%temp%\%1.cmd exit
    start c:\windows\system32\cmd.exe /c "%temp%\%1.cmd"
    goto :eof

:_WAIT
    if +%1+==++ goto :eof
    ping 127.0.0.1 -n %~1 1>nul  2>nul
    goto :eof

:_HELP
    echo Help
    echo shutdown/reboot list of machines
    echo.
    echo.
    echo Ex. Single Pc
    echo call :_SHUTDOWN 30.202.x.y
    echo Abort shutdown (if is in time ...) :_SHUTDOWN_ABORT
    echo call :_SHUTDOWN_ABORT 30.202.x.y
    echo Spegnimento da lista
    echo call :_SHUTDOWN_from_list "c:\pc\listPCs.txt"
    echo Abort shutdown form list
    echo call :_SHUTDOWN_ABORT_from_list  "c:\pc\listPCs.txt"
    echo reboot only PC0001
    echo call :_REBOOT PC0001
    echo Reboot all computers in the list
    echo call :_REBOOT_from_list "c:\pc\listPCs.txt"
    timeout 10

:_END

:_EOF