How to Use PowerShell to Restart Computers (All the Ways!)

choubertsprojects

The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

PowerShell is a command line based scripting language meant for system administrators to manage and automate tasks on Microsoft Windows computers. It can also be used by IT professionals in other departments, such as DevOps or database developers. If you want your computer’s services to start up when the operating system boots, then PowerShell will be an excellent tool for managing this process easily and efficiently.

PowerShell is a command-line tool that allows users to automate tasks and scripts. It can be used to restart remote computers. This article will show you how to use PowerShell to restart multiple remote computers. Read more in detail here: powershell script to restart multiple remote computers.

How to Use PowerShell to Restart Computers (All the Ways!)

A system administrator will inevitably have to restart a server or system. Why not use PowerShell to restart the machine (or many computers!) instead of going via the user interface?

In this post, you’ll discover how to use PowerShell to control computer restarts. We’ll go much farther than just using the Restart-Computer cmdlet.

Let’s go!

How to Check in the Registry for a Pending Reboot

Prerequisites

This lesson will walk you through many different ways to utilize PowerShell to restart machines. If you want to follow along, you’ll need the following items:

Using Restart-Computer to Restart a Computer

The PowerShell Restart-Computer cmdlet is the first and most prevalent PowerShell-specific approach. This cmdlet is straightforward to use and comes with a variety of arguments, some of which make script integration a breeze.

This is the most easy technique and go-to option for most PowerShell scripts, as you can see in the sample below.

The following example establishes a connection to SRV1, a remote machine. It restarts the machine using the Force argument to bypass the default confirmation.

SRV1 SRV1 SRV1 SRV1 SRV1 SRV1 SRV1 SRV1 SRV1 SRV1 SRV1 SRV

As illustrated below, the Restart-Machine cmdlet offers a few arguments that may be used to customize how the command interacts with the computer.

  • ComputerName – The name of the system you want to restart. This option accepts remote addresses such as NetBIOS, IP Address, or Fully Qualified Domain Name (FQDN). Use., localhost, or omit the argument for a local system.
  • If there are other users on the system, use Force. This will bring the system to a halt.
  • Wait — If you set this option to true, the prompt and pipeline will be blocked forever (unless paired with the timeout parameter). This is used in combination with the For parameter to poll for the availability of a certain component.
  • Timeout — When used with the Wait argument, this ensures that if there is an issue, the restart does not stop the prompt and pipeline forever.
  • For Example PowerShell may search for a few components that signal a successful restart. By default, Restart-Computer checks to see whether PowerShell is running to determine if the restart was successful. Waiting for WMI or WinRM to become available is another possibility.
  • Delay — While waiting for a remote system to become available, the cmdlet will poll every 5 seconds for the specified component to check. The default delay time duration will be overridden by this argument.

Because the remote system call with the ComputerName argument does not utilize WinRM, you don’t need to worry about whether the local system is setup for WinRM.

Using PowerShell and Invoke-CimMethod to Restart a Computer

Invoke-CimMethod isn’t primarily designed for remotely restarting a machine, but it does so by calling a WIM method. You may remotely reboot a machine using a native PowerShell command, which isn’t as versatile as the Restart-Computer cmdlet.

For this procedure, make sure WinRM is setup and permitted across the remote computer’s Windows firewall.

SRV1 -ComputerName -ClassName ‘Win32 OperatingSystem’ -MethodName ‘Reboot’ Invoke-CimMethod -ComputerName SRV1 -ClassName ‘Win32 OperatingSystem’ -MethodName ‘Reboot’

There are a few parameters to be careful of when using the Invoke-CimMethod.

  • ClassName – The CIM class name to use. The Win32 OperatingSystem class is used in the event of a restart command.
  • ComputerName – You may use any of the following remote address types with the WsMan protocol: NetBIOS, IP Address, or Fully Qualified Domain Name (FQDN). If this option is left blank, COM is used to conduct local activities.
  • MethodName – The method name is the name of the WMI method for the chosen class. You must utilize the Reboot technique while performing a restart operation.

Remotely restarting a computer using shutdown.exe

After moving on from PowerShell-specific cmdlets, we arrive to Windows’ standard built-in executable for restarting a machine. The shutdown.exe program has been around for a long time and contains a lot of features.

Although shutdown.exe is not strictly a PowerShell cmdlet, you may use PowerShell to restart machines by executing it as an executable.

For this procedure, make sure the Remote Registry service is turned on and WMI is permitted over the Windows firewall on the remote machine.

shutdown.exe /m remotecomputer /r /t 0 shutdown.exe /m remotecomputer /r /t 0 shutdown.exe /m remotecomputer /r /t

The parameters for the shutdown command are shown below.

  • r — Restarts a computer after it has been turned off.
  • g — This command is identical to r, except it will also restart any registered apps when it is run. The Windows Resume Manager, which was first introduced in Windows Vista, enables users to gracefully shut down and restart apps that have been registered with the system. The Outlook program, for example, is immediately restarted if it was open when the computer was shut off.
  • e – Write down the cause for the system’s unexpected restart.
  • m – The remote system to restart, using the computername parameter.
  • t – The time in seconds between the commencement of the restart operation and the start of the restart operation. The f (force) parameter is assumed if a value larger than 0 is specified. The default value is 30 seconds, although there is no need to use it.
  • c – A restart message that may be up to 512 characters long and will show onscreen before the shutdown as well as in the Windows event log remark.
  • f – Force any open apps to shut, which will prevent any application from prompting for a FileSave prompt and may result in data loss.
  • d – A cause code for the restart procedure is displayed. P:2:18, which stands for Operating System: Security Fix, is an example of this sort of reason code (Planned).

PSExec.exe Restarting Computers

psexec.exe, one of the most often used programs in the Sysinternals toolbox, has numerous unique capabilities that make interfacing with a remote system simple. psexec.exe establishes a service on the remote system that commands are then proxied via, which is distinct from both PowerShell and built-in tools.

Ascertain that the SMB Service is operating, that file and printer sharing is turned on, that basic file sharing is turned off, and that the admin$ administrative share is accessible for this technique.

psexec.exe -d -h remotecomputer “shutdown.exe /r /t 0 /f” psexec.exe -d -h remotecomputer “shutdown.exe /r /t 0 /f”

  • d – Use psexec without waiting for the process to finish, which is handy in scripts.
  • h – If the target system is Vista or above, use the account’s elevated token if one is available.
  • n – When connecting to a remote computer, specifies a timeout in seconds.
  • s — Run the process as the system account, which has many more privileges than the standard administrative account. This is a highly valuable talent to have, even if it isn’t always required or employed.
  • computer – A positional argument in the form of remotecomputer that enables psexec to be used on a remote machine.
  • cmd — A positional argument that specifies the actual command to execute against the system. Because psexec does not perform system restarting, another program, such as shutdown.exe, will be utilized to restart the system.
  • parameters – Any arguments that must be supplied to the previously specified cmd are included in this positional parameter.
  • accepteula — If psexec hasn’t detected that you’ve agreed to the EULA, the utility will usually provide an on-screen prompt to do so. This will allow you to accept the EULA without having to go through the graphical interface.
  • nobanner — When connecting to a remote system, the tool displays banner information, which nobanner suppresses.

Bonus Techniques!

The techniques listed below are not widely utilized, although they may be beneficial depending on the situation.

RunDLL32.exe

Rundll32.exe allows you to use internal executables and Windows APIs, such as shell32.dll, to perform certain operations. You may use this capability to restart a machine in two ways.

  • user.exe rundll32.exe ExitWindowsExec – ExitWindowsExec is a command that restarts the local system.
  • rundll32.exe shell32.dll,SHExitWindowsEx 2 – Restarts the local computer as well.

This approach cannot be used remotely by itself, however it may be used with PowerShell on a remote machine using an Invoke-Command.

# Method 1 Invoke-Command -ComputerName $ComputerName -ScriptBlock { & RunDLL32.exe user.exe ExitWindowsExec } # Method 2 Invoke-Command -ComputerName $ComputerName -ScriptBlock { & RunDLL32.exe shell32.dll,SHExitWindowsEx 2 }

Taskkill.exe

Finally, taskkill.exe is a Windows application that, while in a roundabout fashion, provides some capabilities for restarting machines. You may force a Windows restart by terminating the lsass.exe process.

/S remotecomputer /IM taskkill.exe /F lsass.exe

Restarting Multiple Systems at the Same Time

At some time, most system administrators will need to restart several systems. Let’s have a look at how you may be able to achieve this by combining these instructions.

Using PowerShell 7 and the Restart-Computer function, you can easily restart several PCs in simultaneously.

$ComputerArray | ForEach-Object -Parallel Restart-Computer -ComputerName $ -Force -ThrottleLimit 3 Restart-Computer -ComputerName $ -Force

It’s simple to add parallelization to your commands, as you can see with the newest ForEach-Object -Parallel ability. When paired with ForEach-Object, Restart-Computer provides native remote restarting, making maintaining and restarting huge systems a breeze!

The “powershell restart-computer after 30 seconds” is a PowerShell command that allows users to restart their computers. This can be done by typing the command into a PowerShell window or by running it from the Windows Start menu.

Frequently Asked Questions

How do I restart my computer in PowerShell?

A: You can restart your computer with the command shutdown -r

How do I restart a remote computer using PowerShell?

A: You can restart a remote computer using PowerShell by typing the following at your command prompt. Restart-Computer -Force

How do I force a remote computer to restart?

A:
There are 3 ways to reset a remote computer.
The easiest way is by using the shutdown command for the remote computer on its local terminal, which will restart it automatically when youre done. You can also use nc (network connection) in order to connect directly and run commands remotely over SSH or telnet. Finally, if your MacOS has an emergency feature that allows you to force shut down whats already running with CMD + ALT+ SHIFT+ ESC, this option would also suffice as well.

Related Tags

  • powershell restart-computer other users logged in
  • powershell script to restart-computer
  • restart-computer access denied
  • restart remote computer cmd
  • powershell restart local computer with delay

Table of Content