
How to shut down computers in PowerShell
July 29, 2021
Stop-Computer cmdlet
The Stop-Computer cmdlet shuts down the local or remote computer.
You can run Stop-Computer with additional options to specify authentication types and alternate credentials, and to force an immediate shutdown.
This cmdlet uses the Win32Shutdown method of the Win32_OperatingSystem WMI class.
The following two commands are identical and shutdown the local computer:
Stop-Computer Stop-Computer -ComputerName localhost
The following example will shutdown a computer named Win-Server-Core that has an Administrator user with sufficient rights to shutdown the computer:
Stop-Computer -ComputerName Win-Server-Core -Credential Administrator
This command shuts down remote computers and the local computer.
Stop-Computer -ComputerName "Server01", "Server02", "localhost"
In the previous command, Stop-Computer uses the -ComputerName option to specify two remote computers and a local computer. Each of the listed computers will be turned off.
The following code shuts down remote computers as a background job:
$j = Stop-Computer -ComputerName "Server01", "Server02" & $results = $j | Receive-Job $results
In the previous example, Stop-Computer uses the -ComputerName option to specify two remote computers. The background operator “&” translates the command as a background job. The job objects are stored in the $j variable.
The job objects in the $j variable are piped to Receive-Job, which receives the job results. The objects are stored in the $results variable. The $results variable displays information about the job in the PowerShell console.
Shutting down the remote computer:
Stop-Computer -ComputerName "Server01" -WsmanAuthentication Kerberos
In the previous example, Stop-Computer uses the -ComputerName option to specify the remote computer. The -WsmanAuthentication option specifies using Kerberos to establish a remote connection.
Shutting down a computer in the domain:
$s = Get-Content -Path ./Domain01.txt $c = Get-Credential -Credential Domain01\Admin01 Stop-Computer -ComputerName $s -Force -Credential $c
In the previous example, Get-Content uses the -Path option to read the contents of the Domain01.txt file in the current directory with a list of domain computers. The objects are stored in the $s variable.
Then Get-Credential uses the -Credential option to specify the credentials of the domain administrator. The credentials are stored in the $c variable.
Finally, Stop-Computer shuts down the computers specified in the list of computers in the -ComputerName option in the $s variable. The -Force option causes immediate shutdown. The -Credential option passes your credentials stored in the $c variable.
Error “Unable to initiate a system shutdown because the computer is being used by other users”
Running command
Stop-Computer -ComputerName Win-Server-Core -Credential Administrator
may fail with the error message:
Stop-Computer: Failed to stop the computer Win-Server-Core with the following error message: Unable to initiate a system shutdown because the computer is being used by other users
Its reason is that users are logged in to the computer and are using it.
To force shutdown, specify the -Force option:
Stop-Computer -ComputerName Win-Server-Core -Credential Administrator -Force
Shutting down the computer without PowerShell
Without PowerShell, you can shutdown your computer with the following command:
shutdown /s
You will be shown a warning and the computer will shut down after 30 seconds.
To shut down the computer immediately, run the command:
shutdown /s /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 (100%)
- How to manage services on Windows (100%)
- How to set IP address, netmask, default gateway and DNS for a network interface in PowerShell (100%)
- How to configure the network interface to use a dynamic IP address (DHCP) in PowerShell (100%)
- How to restart computers in PowerShell (100%)
- How to download YouTube subtitle file in any language (RANDOM - 50%)