In one of my previous posts, I talked about how to shutdown a remote computer gracefully. It was useful and Powershell since it allows you to give comment etc. But the only problem is, we always don’t want to run big scripts for simple tasks. I need just a one liner when I want to shutdown the computer remotely. Of course we can use shutdown.exe but there is a powershell way too. The Stop-Computer can be used to accomplish this task. The advantage with using powershell cmdlet is, you can pipe computer names as input or provide multiple computer names as argument.
Here is a small example.
Stop-Computer -ComputerName PC1
This might fail if you have someone logged on to the computer. In such cases you will receive below error.
PS C:\> Stop-Computer -ComputerName PC1
Stop-Computer : This command cannot be run on target computer(‘PC1’) due to following error: The system shutdown
cannot be initiated because there are other users logged on to the computer.
At line:1 char:1
+ Stop-Computer -ComputerName PC1
+ CategoryInfo : InvalidOperation: (PC1:String) [Stop-Computer], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StopComputerCommand
The solution in this is to use -Force switch to the above command.
Stop-Computer -ComputerName PC1 -Force
This should work in any case. Note that you have enough mechanism(like WOL) in place before you shutdown remote computers. Otherwise you have to go manually and swtich them ON 🙂
Hope this helps