
How to restart computers in PowerShell
July 29, 2021
Restart-Computer Cmdlet
The Restart-Computer cmdlet restarts the operating system on a local or remote computer.
The following command will reboot the local computer:
Restart-Computer
You can use Restart-Computer with various parameters to trigger restart operations, to specify authentication levels and alternate credentials, to restrict operations to be performed simultaneously, and to restart immediately.
Starting with Windows PowerShell 3.0, you can wait until the reboot is complete before running the next command. Specify the timeout and request interval and wait for certain services to be available on the rebooted computer. This feature makes it practical to use Restart-Computer in scripts and functions.
Restart-Computer only works on Windows computers and requires WinRM and WMI to shut down the system, including the local one.
Example of restarting multiple computers:
Restart-Computer -ComputerName Server01, Server02, localhost
The -ComputerName <String[]> option specifies a single computer name or an array of computer names separated by commas. Restart-Computer accepts ComputerName objects from a pipeline or variables.
Enter the NetBIOS name, IP address, or fully qualified domain name of the remote computer. To specify the local computer, enter the computer name, period “.” or “localhost”.
This parameter is independent of PowerShell remoting. You can use the -ComputerName parameter even if your computer is not configured to run remote commands.
If the -ComputerName parameter is not specified, Restart-Computer restarts the local computer.
If you are restarting a remote computer, you will most likely need the -Credential <PSCredential> option, which specifies a user account that has permission to perform this action. By default, this is the current user.
Enter a username, such as User01 or Domain01\User01, or enter the PSCredential object generated by the Get-Credential cmdlet. If you enter a username, you will be prompted for a password.
The credentials are stored in the PSCredential object (/dotnet/api/system.management.automation.pscredential) and the password is stored as SecureString (/dotnet/api/system.security.securestring).
The following example reboots computers whose names are obtained from a text file:
Get-Content -Path C:\Domain01.txt | Restart-Computer
Get-Content uses the -Path parameter to get a list of computer names from the Domain01.txt text file. Computer names are sent down the pipeline. Restart-Computer restarts every computer.
Forced restart of computers listed in the text file:
$Names = Get-Content -Path C:\Domain01.txt $Creds = Get-Credential Restart-Computer -ComputerName $Names -Credential $Creds -Force
Get-Content uses the -Path parameter to get a list of computer names from the Domain01.txt text file. Computer names are stored in the $Names variable. Get-Credential prompts you for a username and password and stores the values in the $Creds variable. Restart-Computer uses the -ComputerName and -Credential parameters with their variables. The -Force option causes each computer to restart immediately.
Restarting the remote computer and waiting for it to turn on to execute PowerShell:
Restart-Computer -ComputerName Server01 -Wait -For PowerShell -Timeout 300 -Delay 2
Restart-Computer uses the -ComputerName parameter to restart Server01. The -Wait parameter makes the command wait for the restart to complete. -For sets PowerShell to run commands on the remote computer. The -Timeout parameter sets a five minute wait. The -Delay parameter polls the remote computer every two seconds to determine if it has restarted.
Restarting your computer using WsmanAuthentication:
Restart-Computer -ComputerName Server01 -WsmanAuthentication Kerberos
Restart-Computer uses the -ComputerName parameter to restart the Server01 remote computer. The -WsmanAuthentication parameter specifies the Kerberos authentication method.
Error “Unable to initiate a system shutdown because the computer is being used by other users”
When restarting a remote computer, for example:
Restart-Computer -ComputerName Win-Server-Core -Credential Administrator
an error may occur
Restart-Computer: Failed to restart the computer Win-Server-Core with the following error message: System shutdown cannot be initiated because the computer is in use by other users.
This error occurs if any user is logged in to the remote computer, use the -Force option to force reboot:
Restart-Computer -ComputerName Win-Server-Core -Credential Administrator -Force
Restarting the computer without PowerShell
Without PowerShell, you can restart your computer with the following command:
shutdown /r
You will be shown a warning and the computer will restart after 30 seconds.
To restart your computer immediately, run the command:
shutdown /r /t 0
You can add the /f option to the command, which means forcibly closing running applications without warning users. The /f parameter is implied if the /t parameter is set to a value greater than 0.
Related articles:
- How to display all environment variables at the Windows command prompt (52.2%)
- How to manage services on Windows (52.2%)
- How to set IP address, netmask, default gateway and DNS for a network interface in PowerShell (52.2%)
- How to configure the network interface to use a dynamic IP address (DHCP) in PowerShell (52.2%)
- How to shut down computers in PowerShell (52.2%)
- PowerShell: how to find only folders with Get-ChildItem (RANDOM - 52.2%)