Many times, a simple restart is enough to solve complex computer problems such as crashed applications, unresponsive OS or screen, and instability in operations. When you ask a geek how to solve a particular problem, the first thing they ask you to do is restart the computer! So, why does a simple restart solve so many complex problems?
First off, everything is programming, starting from your Windows OS to the smallest of applications and the program always can encounter an error and stop working. When you restart the device, the code is executed right from the first line again and hopefully, it won’t encounter the problem it did earlier.
Also, memory leaks and CPUs draining your resources are common problems that are best fixed with a reboot as it will clean the slate and start everything fresh. Essentially, when you restart your computer, any code that is caught in an unstable state is eliminated, and this explains why restarting your computer is such a great fix for many problems that are otherwise hard to spot.
There are many ways to restart a computer, and one such way is to use PowerShell.
Using PowerShell restart
Often, PowerShell is seen as an enhanced version of the command prompt as it is based on the .NET framework and is supported by a scripting language as well. As a result, you can reboot a computer from a program when it encounters certain problems instead of expecting the user to do it manually.
The cmdlet is:
This cmdlet restarts not just your local systems but also gives the flexibility to restart remote network computers as well, especially from within a program if needed. As soon as you execute this command, Windows will start a five-second countdown after which it will restart your computer.
Another advantage with this command is that you can continue with your programming as the next command will be executed automatically after the system restarts. You can even specify a waiting time-out and possibly a query interval as well, so certain services will start on the restarted system according to your needs. This aspect is what makes PowerShell restart such a powerful tool for every programmer.
Note that this cmdlet doesn’t generate any output.
Using PowerShell restart on a single computer
As we mentioned, to restart a single computer and a local one, your cmdlet is Restart-Computer. But a local restart doesn’t allow you to tap into the true power of PowerShell restart as you can do it in other ways too. Still, it is essential if you plan to do a restart from a script or function.
Using PowerShell restart on multiple computers
The real flexibility of the restart cmdlet comes to the fore when you want to reboot multiple systems remotely. You can reboot as many computers on your network as you want through a single command and you can even combine local and remote computers in your list. One caveat: You may have to tweak your firewall rules to make sure the command will be executed.
Restart-Computer -ComputerName comp1, comp2, comp3, localhost
In the above example, you’re restarting four computers out of which three are remote and the last one is your local computer. The parameter ComputerName also accepts an array of names if that’s something that works easier for you.
Another possibility is you can list down the names of computers that have to be rebooted in a text file and the Restart-Computer cmdlet can read the values from the text file and reboot all the specified computers. The command for that is,
Get-Content -Path C:\computer_names.txt | Restart-Computer
In the above code, you’re getting the content from a file and sending it as input to the Restart-Computer cmdlet. This function truly gives you a ton of flexibility as you can simply change the names of computers in the text file instead of typing all the names manually each time into the command. Also, you’ll have more control over the computers that need a restart.
Force a restart
You can force a computer to restart right away:
Restart-Computer -ComputerName comp1, comp2, comp3, localhost -Force
This parameter forces an immediate restart, regardless of other commands. This can be a bit tricky to use in a real-world situation, so exercise caution while using this parameter.
Continuing after a restart
A cool aspect of the Restart-Computer cmdlet is that it waits for the computer to boot up before continuing with the rest of the code. You can specify how long the program should wait including the timeout and delay period before the code can start executing where it left off. Though this may sound counterintuitive to the idea of a restart, it still helps to know that something like this is available in PowerShell, in case you want to use it at any time.
Sample code for this feature is:
Restart-Computer -ComputerName comp1 -Wait -For PowerShell -Timeout 300 -Delay 2
In this code, you’re asking the computer to restart comp1, wait for the restart to finish after which you allow PowerShell to run commands remotely on comp1. Also, you’re asking the program to wait for five minutes for the computer to restart and you’re asking it to check if the computer has restarted once every two seconds. The above code is a powerful one that can truly help you make the most of PowerShell’s flexibility.
Using WsmanAuthentication parameter
An interesting parameter you can use in Restart-Computer is WsmanAuthentication.
This parameter allows you to specify the authentication mechanism that must be used to authenticate user credentials. The possible values it can take are:
- Basic: Uses basic authentication to establish a remote connection. This is not the safest choice as the credentials are sent across the network in a text file.
- Credssp: Uses the Credssp authentication
- Default: Uses the default authentication that is defined by the underlying protocol for establishing a connection.
- Digest: The Digest authentication mechanism is similar to the basic one, except that it sends the credentials as a hash value known as a message digest. This is a more secure way of transmission as the username and password are hard to decipher from the hash value.
- Kerberos: Uses Kerberos mechanism for authentication
- Negotiate: Uses the negotiate mechanism for authentication
So far, we have seen a lot of the common parameters that are used with PowerShell restart. Here are the remaining ones.
- -Confirm: Asks the user to confirm before restarting the computer
- -Credential: Specifies the user account that has the permission to do a restart
- -WhatIf: Simulates a restart so you know what would happen. Note that the Restart-Computer cmdlet is not executed.
As you can see, PowerShell’s Restart-Computer is a powerful command that allows you to reboot local and remote network computers from a script. It gives a lot of flexibility such as restarting multiple computers, using a specific authentication method, and above everything, enables you to continue your work after the computer restarts. These aspects make it a truly versatile option to have in your arsenal.
Featured image: Shutterstock
More PowerShell Basics articles
- PowerShell print: Output at your fingertips, to screen or file
- Output contents to a file with PowerShell: All you need to know
- How to read a text file in PowerShell quickly and easily
- Creating files and folders the easy way with PowerShell
- Check domain controllers audit configuration with this PowerShell script