When there is a need to restart a remote computer then there are several ways one can restart, however, this article explains an easy method using a single PowerShell command to restart multiple remote computers, let’s get started.
Restart-Computer
This PowerShell command reboots the local computer or remote computers gracefully, it has got some important parameters for restart.
SYNTAX
Restart-Computer [[-ComputerName] <String[]>] [[-Credential] <PSCredential>] [-AsJob] [-Confirm]
How to Restart A Single Computer
Here is the example when you want to restart a single computer, will discuss the multiple computers restart after this.
Example
Restart-Computer -ComputerName mywindows10
Restart-Computer -ComputerName winserver2016 -force
How to Restart Multiple Computers
This below scripts restart multiple computers, all the computers will be restarted at once if you use -asjob parameter at the end.
Example Script
We need to create a text file for remote computers list that we are going to restart, here I have created a text file named as allservers.txt and saved it in location C\temp\allservers.txt
$allservers =get-content -path "c:\temp\Allservers.txt" Foreach ($oneserver in $allservers) { Write-Host "Currently The Script is Running on" $oneserver Restart-Computer -ComputerName $oneserver }
The above simple script restarts all the remote computers listed in the text file and gives you the information as to which computer is being restarted if you want to restart all at once and no information should be required to display during the reboot then use the below code.
$allservers =get-content -path "c:\temp\Allservers.txt" Foreach ($oneserver in $allservers) { Restart-Computer -ComputerName $oneserver -AsJob }
Restart-Computer Parameters Use
-ComputerName string[]
This parameter is used for declaring One or more remote computer name and it takes the default computer as the local computer if you don’t specify any computer name.
As for computer name, you can provide the hostname, an IP address, NETBIOS name, or a DNS fully qualified domain name works too.
-Force
This parameter forces the command to restart a computer without asking the user confirmation.
-AsJob
This parameter Runs the command as a background job.
One has to declare this parameter at the end and When this parameter is used, Restart-Computer command immediately initiates a background job, works in the background and user can use the PowerShell session for other scripts or other tasks.
These jobs will be created on the local computer and execution happens on the remotes computers. When you want to see running jobs then there is a command get-job –state running.
-WhatIf
This parameter shows what happens when you execute a command, I strongly recommend testing this before you restart multiple computer’s at once.
-Confirm
This parameter prompts user confirmation before it restarts the computer.
-Credential
If you want to restart computers using different accounts or credentials then we have to use this parameter, basically, it runs with default current user account.
Example 1
A single line script for restarting multiple computers
Restart-Computer -ComputerName mywindows10, win10app, computer4, 192.168.1.100
Example 2
A single line script for restarting multiple computers with -force parameter
Restart-Computer -ComputerName mywindows10, win10app, computer4, 192.168.1.100 -force
Example 3
A single line script for restarting multiple computers with -AsJob parameter
Restart-Computer -ComputerName mywindows10, win10app, computer4, 192.168.1.100 -force -AsJob
Thank you for reading this article, if you have any questions please let us know.
Thank you for visiting my site, for any scripts in these articles you are testing please make sure you have tested this script in our lower environment before you run in production.
Leave a Reply Cancel reply