I will show you here how to restart a computer using PowerShell. We will see different cmdlets with examples.
To restart a computer using PowerShell, you can use the Restart-Computer cmdlet. For a local restart, simply run Restart-Computer in an elevated PowerShell session. To restart a remote computer, use Restart-Computer -ComputerName “RemoteComputerName” and provide credentials if necessary.
Restart a Computer Using PowerShell
Now, let me show you how to use different methods to restart a computer using PowerShell.
Method 1: Using the Restart-Computer Cmdlet
The Restart-Computer cmdlet is the best way to restart a computer using PowerShell. This cmdlet is available on the Windows platform and can be used to restart both local and remote computers.
Restart the Local Computer
To restart the local computer, simply open PowerShell with administrative privileges and run the following command:
Restart-ComputerThis command will prompt the computer to restart immediately.
Restart a Remote Computer
To restart a remote computer, specify the computer name and ensure you have the necessary permissions. Use the following command:
Restart-Computer -ComputerName "RemoteComputerName"Replace "RemoteComputerName" with the actual name of the remote computer. You may also need to provide credentials if you are not running the command as an administrator:
Restart-Computer -ComputerName "RemoteComputerName" -Credential (Get-Credential)This command will prompt you to enter the credentials for the remote computer.
Check out How to Use -and Operator in PowerShell?
Method 2: Using the shutdown.exe Command
Another method to restart a computer is by using the shutdown.exe command, which can be called from within PowerShell. This method is useful if you encounter issues with the Restart-Computer cmdlet.
Restart the Local Computer
To restart the local computer using shutdown.exe, run the following command in PowerShell:
shutdown.exe /r /t 0/r: Specifies a restart./t 0: Sets the time delay to 0 seconds.
Restart a Remote Computer
To restart a remote computer using shutdown.exe, use the following command:
shutdown.exe /r /m \\RemoteComputerName /t 0Replace \\RemoteComputerName with the name of the remote computer.
Method 3: Restart with a Warning Message
If you want to provide users with a warning message before restarting, you can use a script to display a pop-up message and then restart the computer. Here’s an example:
Add-Type -AssemblyName PresentationFramework
[System.Windows.MessageBox]::Show('The computer will restart in 5 minutes. Please save your work.', 'Restart Warning', 'OK', 'Warning')
Start-Sleep -Seconds 300
Restart-ComputerThis script will display a warning message and wait for 5 minutes before restarting the computer.
Check out PowerShell -contains Operator
Method 4: Restart Multiple Computers
To restart multiple computers simultaneously, you can use a loop in your PowerShell script. Here’s an example:
$computers = @("Computer1", "Computer2", "Computer3")
foreach ($computer in $computers) {
Restart-Computer -ComputerName $computer -Force
}This script will restart all computers listed in the $computers array.
Conclusion
As a PowerShell administrator, you might need to restart a computer using PowerShell. I have explained here how to restart a computer using PowerShell using different methods like:
- Using the Restart-Computer Cmdlet
- Using the shutdown.exe Command
- Restart with a Warning Message
- Restart Multiple Computers
You may like the following tutorials:
- PowerShell ForEach-Object vs ForEach
- PowerShell ForEach-Object
- PowerShell Switch Case with Regex
- How to Get Computer Name in PowerShell?
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.