Restarting or Shutting Down a Server

When you shut down or restart a Windows server, a signal is sent to all devices, services, and programs running on the system. This signal announces that the system is preparing to shut down so everything needs to close gracefully, if possible. A normal shutdown will wait for a response from all devices and processes to make sure they have saved any files and can close before proceeding with the shutdown. If you’ve ever had Notepad or Word open with an unsaved file, you’ve seen the message asking if you want to save the file before the application closes.

If you need to automate the shutdown or restart of a system, then politely asking each application to close can be problematic. If someone had an open unsaved file on the target system, the shutdown wouldn’t proceed as expected. That’s why Windows also supports forcefully shutting down a system. With the shutdown.exe command, you can specify an additional option (/f on Windows Server 2003 and /c on Windows 2000) to force all applications to close. If an application like Notepad is running, it automatically closes any unsaved files and your unsaved changes will be lost.

On the programmatic side, the Win32_OperatingSystem class supports two shutdown methods. The first one (Shutdown), performs a normal shutdown. It will not forcefully close files. The second, however, Win32Shutdown, will.

So you can perform a forceful shutdown with a command line and from a script. How about from the GUI? You sure can. Just hit Ctrl+Alt+Del on your keyboard. Using the Tab key move to the Shut Down button. Hold the Ctrl key down and click Enter. Click OK to confirm the shutdown.

One other thing that you may need to do at some point is schedule a server to reboot or shutdown at a specific time. This is sometimes needed if an entire network or data center needs to move or go down for maintenance. You could automate this job pretty quickly using a combination of the scripting solutions and the Task Scheduler, but there is another way using the command line. The at.exe command lets you schedule a task to run either locally or remotely at a specified time.

This command causes a server to shut down at 20:00 (8 P.M.) tonight:

> at \\ 20:00 shutdown.exe /s /c "Datacenter move"

This command causes the server to restart at 21:00 (9 P.M.) every Friday night:

> at \\ 21:00 /every:F shutdown.exe /r /c "Weekly restart"

Using a graphical user interface

With Windows Server 2003 and Windows 2000 you can obviously shut a machine down by going to Start > Shut Down, but I’ll describe the new graphical interface with the shutdown.exe command, which is available in Windows Server 2003.

  1. From the Start menu select Run.
  2. Type cmd.exe and click OK.
  3. In the CMD window, type shutdown /i and hit enter.
  4. Click the Add button, type the names of the server(s) you want to shut down or restart, and click OK.
  5. Select whether you want to restart or shut down the server(s).
  6. Enter the number of seconds to warn logged on users (or uncheck to not warn).
  7. Configure the Shutdown Event Tracker options.
  8. Click OK. In the CMD window, you’ll see a status message stating if the operation was successful.

Here is another option that is available from either Windows 2000 or Windows Server 2003:

  1. Open the Computer Management snap-in.
  2. If you want to target a remote system, right-click on the Computer Management icon in the left pane and select Connect to another computer. Enter the computer name beside Another computer and click OK.
  3. Right-click on the Computer Management icon in the left pane and select Properties.
  4. Click the Advanced tab.
  5. Under Startup and recovery, click the Settings button.
  6. Click the Shut Down button.
  7. Select from the list of actions and options under Forced Closed Apps.
  8. Click OK.

Using a command-line interface

The following two commands work with the Windows Server 2003 version of shutdown.exe. This shuts a server down after the 30 seconds (default wait timer):

> shutdown /m \\ /s /c "Server requires reboot due to app install"

This command restarts a server after 20 seconds:

> shutdown /m \\ /r /t 20 /c "Server is going down for repairs"

You can use the /f option to force applications to close.

On Windows 2000, the shutdown options are a little different. This command shuts down a server (in 30 seconds by default):

> shutdown \\ "Server is going down for repairs"

And this restarts (/r option) a server in 15 seconds:

> shutdown \\ /r /t:15 "Server requires reboot due to app install"

You can force all applications to close by using the /c option.

Using VBScript

' This code shuts a server down.
 ' ------ SCRIPT CONFIGURATION ------
 strComputer = ""   ' e.g., rallen-srv01
 ' ------ END CONFIGURATION ---------
 set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 set colOS = objWMI.InstancesOf("Win32_OperatingSystem")
 for each objOS in colOS
    intRC = ObjOS.Shutdown( )
    if intRC <> 0 then
       WScript.Echo "Error attempting to shutdown server: " & intRC
    else
       WScript.Echo "Shutting down server..."
    end if
 next
' This code forcefully shuts a server down.
 ' ------ SCRIPT CONFIGURATION ------
 strComputer = ""   ' e.g., rallen-srv01
 intFlag = 1 + 4    ' Flag for forceful shut down
 ' ------ END CONFIGURATION ---------
 set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 set colOS = objWMI.InstancesOf("Win32_OperatingSystem")
 for each objOS in colOS
    intRC = ObjOS.Win32Shutdown(intFlag)
    if intRC <> 0 then
       WScript.Echo "Error attempting to shutdown server: " & intRC
    else
       WScript.Echo "Shutting down server..."
    end if
 next
' This code reboots a server.
 ' ------ SCRIPT CONFIGURATION ------
 strComputer = ""   ' e.g. rallen-srv01
 ' ------ END CONFIGURATION ---------
 set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 set colOS = objWMI.InstancesOf("Win32_OperatingSystem")
 for each objOS in colOS
    intRC = ObjOS.Reboot( )
    if intRC <> 0 then
       WScript.Echo "Error attempting to reboot server: " & intRC
    else
       WScript.Echo "Rebooting server..."
    end if
 next