Shutting down or restarting the server

I just couldn't resist starting with this one. Yes, this seems trivial. Silly even. However, the number of times that I have watched a simple server restart consume more mouse clicks than creating a domain controller has convinced me that this needed to be in this book. Perhaps the shutdown and restart options were hidden away purposefully, because once your system is up and running, there is not often a need to accomplish either of these tasks. When first configuring the box, though, it is very common to have to reboot a couple of times or shut down a machine to move it to another location. Let's face it, it doesn't seem to matter how many years computers have been around – sometimes, the magical reboot process is still the fix.

Getting ready

To complete this recipe, you will need a Windows Server 2019 system online. There are no other prerequisites.

How to do it…

Let's take a look at three different ways to shut down or restart your system. The first is going to be the most commonly employed. The second is still being used by quite a few folks who had to work hard at getting this strange location in their heads during the Windows 8 rollout, and they have continued to use it from that point forward. The third is less commonly known but is by far my favorite when tasked with restarting a remote server.

Using the Start menu

The first option, thankfully, is in a location that makes sense to anyone using Windows 10. We can simply click on the Start button, and see right there, near the bottom, that we have Power control options available to us:

Figure 1.1 – Power control options in the Start menu

Now, when you click on Shut down or Restart, you will be asked to supply a reason why you are restarting. Common sense tells us that if you are manually clicking on the Restart button, there is a pretty good chance you are actually intending to restart the server, right? A planned occurrence? But what is the default option that presents itself? Other (Unplanned). Alas, this default option is certainly going to cause us log files full of unplanned restarts, even though all of those restarts were actually planned. Because let's be real – nobody takes the time to change that drop-down menu before they click Continue:

Figure 1.2 – Shut down/Restart prompt, asking users to choose a reason for shutting down or restarting

The second method to accomplish shutting down or restarting is by right-clicking on the Start button. We will discuss this little menu that is presented when right-clicking on Start in our next recipe, but for the sake of a quick shutdown or restart, you can simply right-click on the Start button and then choose Shut down or sign out:

Figure 1.3 – The shutdown or sign out prompt in the Start menu

These two examples run the risk of rebooting the wrong system. Depending on how many layers of remote connections, such as RDP, you are using, it is fairly easy to reboot your own computer or the wrong server instead of the server you intended to reboot. This is because it is fairly easy to click on the Start button of a different system than the one you intended in the first place. One of the most fool-proof ways of restarting your server is at the Command Prompt. Doing this gives you the opportunity to double-check that you are manipulating the correct machine.

Using the Command Prompt

Open a Command Prompt (make sure you are selecting Run as Administrator) and run a quick hostname check to make sure you are restarting the one you really intend to. Then, utilize the shutdown command to take care of the rest. This process can be especially helpful when you're logged into remote servers using RDP. Use the following commands to perform the explained operations:

hostname

shutdown /r /t 0

If you were to simply type shutdown, the server would shut itself down in 60 seconds. Using /r indicates a restart rather than a shutdown, while /t 0 is a timing flag that indicates the number of seconds the server should wait before restarting. Specifying zero here tells it to wait for zero seconds before initiating the restart.

Using Windows PowerShell

The Command Prompt is very 2003. As mentioned in the introduction, we're going to start doing things the PowerShell way. So, instead of opening a Command Prompt, we'll open PowerShell instead (you will need to right-click on PowerShell in the Start menu and choose Run as Administrator). Then, run these commands:

hostname

Restart-Computer

As you can see, the first line, hostname, is the same as it was in our Command Prompt from before. That's because almost any command that works in the Command Prompt also works in PowerShell. However, our second command is different. Restart-Computer is a PowerShell command that does the same thing as shutdown /r /t 0 but is much easier to remember. As a bonus, you can also restart other servers without having to log onto them at all with that command using the -ComputerName parameter:

Figure 1.4 – An example of the output of using the hostname command in Windows PowerShell

Windows also has a Stop-Computer command, which you can use if you wish to shut down a server instead of restarting it – but be aware that once the machine is shut down, you will be unable to use PowerShell to start it back up!

Tip

PowerShell has a feature called "tab completion". If you write the first few letters of a command and then press tab on your keyboard, PowerShell will try to finish the command for you. Try just typing the letters "Restart" and press tab. PowerShell should auto-complete this to Restart-Computer for you.

How it works…

Shutting down or restarting a server doesn't require a lot of explanation, but I hope that this small recipe has got you thinking about creative ways to perform regular tasks. As you will see throughout this book, you can accomplish anything in Windows Server 2019 through the use of commands, scripts, and PowerShell. You could easily turn the Restart-Computer command, which we explored in the last example that we tested in this recipe, into a script file, and place it on the desktop of each of your servers as a quick double-click option for accomplishing this task.

However, I work with RDP windows inside RDP windows very often. When bouncing around between a dozen servers that all have the same background image, I found that the only sure-fire way to make sure you are restarting the correct device is to do a quick hostname check before you initiate the restart. If you are interested in discovering all of the available flags that are available in PowerShell for restarting your server, make sure to type in Get-Help Restart-Computer sometime to take a look at all of the available options. We'll look at this in more detil in the Searching for PowerShell cmdlets with Get-Help recipe.

Tip

Using PowerShell is also an easy way to log off a server. Let's say you are layers-deep in RDP and want to log off from a single server (not all of them). Are you sure you clicked on the Start button of the right server? Instead, open PowerShell and simply type logoff.