Remote reboot windows powershell

To reboot a remote computer using PowerShell, you can utilize the `Restart-Computer` cmdlet, specifying the remote machine’s name and any necessary credentials.

Restart-Computer -ComputerName "RemotePCName" -Credential (Get-Credential) -Force

Understanding PowerShell Commands

What is PowerShell?

PowerShell is a powerful scripting and automation tool designed by Microsoft. It’s specifically tailored for system administration and it provides administrators with the ability to manage configurations, automate tasks, and perform system management functions efficiently.

One of the significant advantages of using PowerShell is its versatility; it allows access to various management tasks across different devices and systems. With its robust command-line interface, PowerShell empowers users to quickly execute commands, leverage automation scripts, and connect to remote systems seamlessly.

PowerShell Cmdlets for Remote Management

Cmdlets are lightweight commands that are utilized within PowerShell to perform specific functions. These cmdlets not only encapsulate complex actions into simple commands but also streamline workflows by allowing administrators to execute actions on local and remote systems easily.

Key cmdlets related to remote management include:

  • `Invoke-Command` — to run commands on remote machines.
  • `Enter-PSSession` — to enter an interactive session with a remote system.
  • `Restart-Computer` — used for rebooting local or remote computers.

Understanding Microsoft.PowerShell.Commands.Internal.Format.FormatStartData

Understanding Microsoft.PowerShell.Commands.Internal.Format.FormatStartData

Rebooting a Remote Computer with PowerShell

What You Need Before Rebooting Remotely

Before attempting to execute a remote reboot using PowerShell, certain prerequisites must be satisfied:

  • Permissions: Ensure you have administrative access to the remote computer.
  • Network Connectivity: Confirm that the remote computer is reachable over the network.
  • PowerShell Remoting Configurations: Verify that PowerShell Remoting is enabled on the remote machine.

Enabling PowerShell Remoting

To harness the full capabilities of PowerShell for remote management, it’s essential to enable PowerShell Remoting on the target machines. Use the following command to enable remoting:

Enable-PSRemoting -Force

This command allows PowerShell to receive commands from remote sessions and will adjust the firewall settings appropriately.

Firewall Considerations

Ensure that the Windows Firewall on the remote computer permits traffic on the ports used for PowerShell Remoting (by default, TCP port 5985 for HTTP and TCP port 5986 for HTTPS).

Syntax for Remote Reboot Command

To reboot a remote computer, you can use the `Restart-Computer` cmdlet. The basic syntax is as follows:

Restart-Computer -ComputerName "RemotePC_Name" -Force

In the above command:

  • `-ComputerName` specifies the name or IP address of the remote computer.
  • `-Force` is an optional parameter which forces an immediate restart without prompting any confirmation.

Mastering Microsoft.PowerShell.Commands.WriteErrorException

Mastering Microsoft.PowerShell.Commands.WriteErrorException

Advanced Options for Remote Restart

Adding Credentials for Remote Restart

If you need to provide credentials to access the remote machine, you can do so by creating a credential object. Use the `Get-Credential` cmdlet to accomplish this:

$credential = Get-Credential
Restart-Computer -ComputerName "RemotePC_Name" -Credential $credential -Force

In this example, the user is prompted to enter a username and password, which PowerShell will use to authenticate the session.

Using the -Force Parameter

The `-Force` parameter is useful when you want to bypass any prompts that could halt the reboot process. However, exercise caution when using this parameter, as it will terminate any running applications or processes without warning.

Handling Multiple Remote Computers

You can also reboot multiple remote computers in a single command by passing an array of computer names. Here’s how you can do that:

$computers = "RemotePC1", "RemotePC2"
Restart-Computer -ComputerName $computers -Force

This command will initiate a reboot for both specified remote computers simultaneously, optimizing your tasks and saving time.

Get NetAdapter PowerShell: Mastering Network Queries

Get NetAdapter PowerShell: Mastering Network Queries

Post-Reboot Considerations

Verifying Successful Reboot

After issuing a reboot command, it’s important to verify whether the remote computer has come back online. You can check the status with the following command:

Get-ComputerInfo -ComputerName "RemotePC_Name"

This command retrieves information from the specified remote machine, allowing you to confirm its operational status.

Handling Errors and Exceptions

While executing the `Restart-Computer` cmdlet, you may encounter errors due to various reasons. Common issues include:

  • Network connectivity problems.
  • Incorrect firewall settings.
  • Insufficient permissions.

To troubleshoot errors, review the error messages provided by PowerShell and check network settings, permissions, and ensure that PowerShell Remoting is enabled on the target machine.

FilesystemWatcher PowerShell: Track File Changes Easily

FilesystemWatcher PowerShell: Track File Changes Easily

Security Implications of Remote Reboot

Best Practices for Secure Remote Management

Managing remote systems using PowerShell brings about important security considerations. To mitigate risks:

  • Limit remote access to only necessary users and systems.
  • Regularly review permissions and access levels.
  • Disable PowerShell Remoting if it’s not needed.

Audit and Logging

Maintaining logs of remote actions is crucial for security and compliance. Enable PowerShell logging to track commands executed in remote sessions. This can provide a clear audit trail and assist in identifying any unauthorized access or changes.

Remotely Execute PowerShell: A Quick Start Guide

Remotely Execute PowerShell: A Quick Start Guide

Conclusion

Utilizing PowerShell to manage and reboot remote computers offers tremendous efficiency for system administrators. With the right understanding of commands and best practices in place, you can execute remote reboots confidently and securely. This flexibility not only enhances operational efficiency but also supports seamless maintenance of networks and systems. Embrace the power of PowerShell in your remote management tasks to optimize your workflow.

Mastering Import-Module in PowerShell: A Quick Guide

Mastering Import-Module in PowerShell: A Quick Guide

Additional Resources

For further learning on PowerShell, consider exploring the official Microsoft documentation as well as reputable books and online courses focused on mastering PowerShell commandlets and remote management techniques.

There will be times as a Windows Administrator when you will need to reboot or shutdown a remote computer or server.

In this tutorial, I’ll show you two easy methods for rebooting and shutting down remote computers.

The first method uses a built in Windows command and the second method uses PowerShell.

Check it out.

Windows Remote Shutdown Command

Windows systems have a built in shutdown command that can be used to restart or shutdown local and remote computers.

The command is shutdown.

To use this command just open the windows command prompt and type shutdown.

To view the full list of command options type shutdown /? in the CMD window.

There are several command line switches, below I list the most useful options.

/s – Shutdown the computer

/r – restart computer

/m \\computer – Specify the remote computer

/l – Log off

/t xxx – Set the time out period before shutdown to xxx seconds

/c “comment” – Message to display on the screen before restart or shutdown

Now let’s move on to some examples

Remote Desktop Connection Manager | Remote Session Tools
Download 14 day Free Trial

Restart or Shutdown Examples with Command Line

In these examples, I’ll be on PC1 and will initiate a remote restart or shutdown on PC2.

I’ll be using the /r switch in these examples, you can change them to /s to shutdown instead of restart.

Example 1: Restart Remote Computer

By default, this will prompt the remote computer and give it about a minute before it restarts.

shutdown /r /m \\pc2

The pop up below is what a Windows 10 system will display.

Example 2: Restart With a Custom Message

You may want to display a custom message to the logged on users, to do that just use the /c command.

shutdown /m \\pc2  /c "The IT department has initiated a remote restart on your computer"

Below is the pop up on the remote computer with the custom message.

Example 3: Immediate Restart no Countdown

If you want to immediately restart with no countdown or message use this command.

shutdown /r /m \\pc2 /t 0

If you want a longer countdown just specify the seconds /t 60

Example 4: Log user off the remote computer

If you just want to log a user off the remote computer use this command.

shutdown /l /m\\pc2

Restart or Shutdown the Computer with Powershell

Here are a few examples of how you can restart or shutdown computers with PowerShell.

The downside to PowerShell is it doesn’t have as many options as the shutdown command. There is no option to prompt users with a custom message or provide a countdown.

Example 1: Use Powershell to restart a computer

This command will immediately restart a remote computer. The -Force option will force a restart even if a user is logged on.

Restart-Computer -ComputerName REMOTE_COMPUTER_NAME -Force

Example 2: Use PowerShell to shutdown a computer

This command will shutdown a remote computer. Use the -Force to force a shutdown even if a user is logged on.

Stop-Computer -ComputerName REMOTE_COMPUTER_NAME -Force

Example 3: Use PowerShell to restart a list of computers

This is handy if you have several computers to restart. Just list all the computers you want in a text file and add that to the PowerShell command.

restart-computer (get-content c:\work\computers.txt)

Example 4: Use PowerShell to shutdown down two computers

Stop-Computer -ComputerName "Server01", "Server02"

Want an easy way to check windows server uptime and last boot date on all computers? Then check out the tool below.

The AD Pro Toolkit includes 13 tools to simplify Active Directory and computer management. Download a free trial and try it for yourself.

I hope you found this article useful. If you have questions or comments leave them below. You might also like to check out my guide on How to Remotely Log off Windows Users.

Download Windows Speedup Tool to fix errors and make PC run faster

Every now and then, most especially, a system administrator will need to restart a server or system. Usually, you can Remote Shut down or Restart Windows through the graphical user interface – PowerShell provides several methods for rebooting a computer remotely and we will outline the 6 known methods in this post.

A prerequisite to these methods is to ensure that we can contact the remote systems and authenticate as necessary. You also need to verify that a remote system is not pending a reboot.

You’ll need the following:

  • A user account on the remote computer in the local administrator’s group.
  • Windows PowerShell or PowerShell Core.

1] Restart a remote computer with Restart-Computer

This cmdlet is simple to use with flexible parameters. An additional prerequisite for the command to work is, ensure that WinRM is configured and allowed through the remote computer’s Windows firewall and that WMI is allowed through the Windows firewall.

Restart-Computer -ComputerName $ComputerName -Force

To restart multiple computers in parallel, run the following command:

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

2] Restart a remote computer with Invoke-CimMethod

The Invoke-CimMethod works by using a WIM method to reboot the remote system – although, not as flexible as the Restart-Computer cmdlet.

An additional prerequisite for the command to work is, ensure that WinRM is configured and allowed through the remote computer’s Windows firewall.

Invoke-CimMethod -ComputerName $ComputerName -ClassName 'Win32_OperatingSystem' -MethodName 'Reboot'

3] Restart a remote computer with shutdown.exe

The shutdown.exe is the standard built-in executable that Windows offers to restart a system, and it’s not a PowerShell command but offers a robust series of options.

An additional prerequisite for the command to work is, ensure that the remote computer has the Remote Registry service enabled and WMI allowed through the Windows firewall.

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

4] Restart a remote computer with PSExec.exe

One of the most used utilities within the Sysinternals toolkit, psexec.exe offers several unique abilities that make interacting with a remote system easy.

An additional prerequisite for the command to work is, ensure the SMB Service is running, file and printer sharing is enabled, simple file sharing is disabled and the admin$ administrative share is available.

psexec.exe -d -h \\remotecomputer "shutdown.exe /r /t 0 /f"

5] Restart a remote computer with RunDLL32.exe

The rundll32.exe offers a way to run certain methods against internal executables and Windows APIs, such as shell32.dll. There are two methods you can restart a system using this functionality but this method cannot be actually used remotely by itself, you can combine this with PowerShell via an Invoke-Command on a remote system.

Method 1:

Invoke-Command -ComputerName $ComputerName -ScriptBlock { & rundll32.exe user.exe ExitWindowsExec }

Method 2:

Invoke-Command -ComputerName $ComputerName -ScriptBlock { & rundll32.exe user.exe ExitWindowsExec }

6] Restart a remote computer with Taskkill.exe

Last but not the least, taskkill.exe is one other Windows utility that offers some functionality to restart Windows, though in a roundabout way. By ending the lsass.exe process, you will force a Windows restart.

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

That’s it on the six ways to use PowerShell to restart a remote computer!

Obinna has completed B.Tech in Information & Communication Technology. He has worked as a System Support Engineer, primarily on User Endpoint Administration, as well as a Technical Analyst, primarily on Server/System Administration. He also has experience as a Network and Communications Officer. He has been a Windows Insider MVP (2020) and currently owns and runs a Computer Clinic.

Inevitably a system administrator will need to restart a server or system. Rather than stepping through the user interface, why not use PowerShell to restart the computer (or lots of computers!)?

Using PowerShell to restart computers

Using PowerShell to restart computers

In this article, you’re going to learn all the ways to manage computer restarts with PowerShell. We’re going a lot deeper than just using the Restart-Computer cmdlet.

Let’s go!

Prerequisites

This tutorial is going to be a guide taking you through various methods to use PowerShell to restart computers. If you intend to follow along, be sure you have the following:

  • A user account on any computer (local or remote) in the local Adminstrators group
  • Windows PowerShell or PowerShell Core. The tutorial will use Windows PowerShell 5.1.
  • WinRM is configured and allowed through the remote computer’s Windows firewall and that WMI is allowed through the Windows firewall

Using PowerShell to Restart Computers with Restart-Computer

The first PowerShell specific method, and most common, is the PowerShell Restart-Computer cmdlet. This cmdlet is simple to use with flexible parameters, some of which make script integration very easy.

As you can see in the example below, this is generally the most straightforward method and go-to solution for most PowerShell scripts.

The simple example below connects to a remote computer called SRV1. To skip the default confirmation, it uses the Force parameter to restart the computer.

Restart-Computer -ComputerName SRV1 -Force

The Restart-Computer cmdlet has a few parameters to configure how to command interacts with the computer as shown below.

  • ComputerName – The system that you intend to restart. This parameter can take the following remote addresses, NetBIOS, IP Address, or the Fully Qualified Domain Name (FQDN). For the local system use ., localhost, or omit the parameter.
  • Force – Used if other users are currently on the system. This will force a shutdown.
  • Wait – This parameter will block the prompt and pipeline indefinitely (unless paired with the timeout parameter). This works in conjunction with the For parameter to poll for a specific component to become available.
  • Timeout – Used with the Wait parameter, this will ensure that the restart doesn’t block the prompt and pipeline indefinitely if there is a problem.
  • For – There are a few components that PowerShell can look for to indicate a successful restart. By default, Restart-Computer looks to see if PowerShell itself is running to indicate a successful restart operation. Another option is to wait for WMI or WinRM to be available.
  • Delay – By default, the cmdlet will poll every 5 seconds for the defined component to check while waiting for a remote system to become available. This parameter will override that default delay time period.

The ComputerName parameter does not use WinRM for the remote system call, therefore, you don’t need to worry about if the local system is configured for WinRM.

Using PowerShell to Restart Computers with Invoke-CimMethod

Not specifically intended for rebooting a system remotely, Invoke-CimMethod works by using a WIM method to reboot the remote system. Not as flexible as the Restart-Computer cmdlet, you can use PowerShell to restart computers remotely using a native command.

Ensure that WinRM is configured and allowed through the remote computer’s Windows firewall for this method.

Invoke-CimMethod -ComputerName SRV1 -ClassName 'Win32_OperatingSystem' -MethodName 'Reboot'

The Invoke-CimMethod has a few parameters you should be aware of.

  • ClassName – The name of CIM class to use. In the case of a restart command, we are using the Win32_OperatingSystem class.
  • ComputerName – Using the WsMan protocol, you can use any of the following remote address types, NetBIOS, IP Address, or the Fully Qualified Domain Name (FQDN). If this parameter is omitted, then local operations are performed using COM.
  • MethodName – The method name is the WMI method for the class that is being targeted. In the case of a restart operation, you will need to use the Reboot method.

Using PowerShell to Restart Computers Remotely with Running shutdown.exe

Moving on from PowerShell-specific cmdlets, we come to the standard built-in executable that Windows offers to restart a system. The shutdown.exe executable has been around a long time and offers a robust series of options.

Although not technically a PowerShell cmdlet, you can still use PowerShell to restart computers with shutdown.exe by invoking as an executable.

Ensure that the remote computer has the Remote Registry service enabled and WMI allowed through the Windows firewall for this method.

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

Below are the parameters for the shutdown command you should know.

  • r – Restarts a computer after first shutting the system down.
  • g – This is similar to the r command, but will also restart any registered applications upon startup. Introduced in Windows Vista, the Windows Restart Manager allows gracefully shutting down and restarting applications registered with this system. An example would be the Outlook application which is automatically started back up, if it was open initially upon shutdown.
  • e – Document the reason for an unexpected restart of the system.
  • m – The remote system to restart, takes the parameter of \\computername.
  • t – The time in seconds before initiating the restart operation. If a value greater than 0 is defined, the f (force) parameter is implied. The default value is 30 seconds, but no force.
  • c – An optional restart message that will appear on-screen before the shutdown and also in the Windows event log comment and can be up to 512 characters.
  • f – Force any running applications to close, which will not prompt for a File→Save prompt in any application and may cause data loss.
  • d – Listing of a reason code for the restart operation. An example of this type of reason code is P:2:18 which corresponds to Operating System: Security fix (Planned).

Using PowerShell to Restart Computers with PSExec.exe

Using PowerShell to restart computers is through one of the most used utilities within the Sysinternals toolkit, psexec.exe offers several unique abilities that make interacting with a remote system easy. Taking a different approach than both PowerShell and built-in utilities, psexec.exe creates a service on the remote system that commands are then proxied through.

Ensure the SMB Service is running, file and printer sharing is enabled, simple file sharing is disabled and the admin$ administrative share is available for this method.

psexec.exe -d -h \\remotecomputer "shutdown.exe /r /t 0 /f"
  • d – Use psexec non-interactively by not waiting for the process to terminate, useful in scripts.
  • h – If the target system is Vista or higher, run this process using the account’s elevated token, if available.
  • n – Specifies a timeout in seconds while connecting to a remote computer.
  • s – Run the process using the system account, which confers a much higher level of access than the typical administrative account. This is not always needed, or used, but a very useful ability to have.
  • computer – A positional parameter that takes the form of \\remotecomputer this allows usage of psexec against a remote system.
  • cmd – Another positional parameter that is used to specify the actual command to run against the system. Since psexec does not do the rebooting of the system itself, this command will be another utility such as shutdown.exe used to restart a system.
  • arguments – This positional parameter contains any arguments that are needed to be passed to the previously defined cmd.
  • accepteula – If psexec has not recorded that you have accepted the EULA, the tool would typically show an on-screen prompt to accept the EULA. This will accept that EULA without the graphical prompt.
  • nobanner – When connecting to a remote system, there is banner information displayed from the tool, which nobanner will suppress.

Bonus Methods!

The methods shown below are not very commonly used but could be useful depending on the needs.

RunDLL32.exe

The rundll32.exe offers a way to run certain methods against internal executables and Windows APIs, such as shell32.dll. There are two methods you can restart a system using this functionality.

  • rundll32.exe user.exe ExitWindowsExec – Restarts the local system.
  • rundll32.exe shell32.dll,SHExitWindowsEx 2 – Will also restart the local system.

This method cannot be actually used remotely by itself, but you can combine this with PowerShell via an Invoke-Command on a remote system.

# 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 one other Windows utility that offers some functionality to restart computers, though in a roundabout way. By ending the lsass.exe process, you will force a Windows restart.

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

Using PowerShell to Restart Computers (Multiple Systems in Parallel)

Most system administrators will need to restart multiple systems at one point or another. Let’s take a quick look at how one might be able to tie these commands together to do this.

Using PowerShell to Restart Computers in parallel is easy using PowerShell 7 and the Restart-Computer command.

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

As you can tell with the newer ForEach-Object -Parallel ability, it’s trivial to add parallelization onto your commands. Since Restart-Computer supports remote restarting natively when combined with ForEach-Object the ease of managing and restarting large systems can quickly be done!

  • Updated on February 14, 2021
  • Powershell, Windows Server

Why do we need to restart a remote computer with PowerShell? We had to test new software, and the computer stopped responding after clicking the File Explorer. The problem is that we could not end any tasks. The computer is hours away from us, and nobody was in the area. What is the best solution? The best approach is to restart the computer remotely. In this article, you will learn how to restart a remote computer with PowerShell.

Table of contents

  • Restart remote computer with PowerShell
  • Restart remote computer with PowerShell Force
  • Ping remote computer
  • Conclusion

Run PowerShell as administrator. We are going to make use of the Restart-Computer cmdlet. The computer’s name is PC01.

PS C:\> Restart-Computer -ComputerName "PC01"
Restart-Computer : Failed to restart the computer PC01 with the following error message: The system shutdown can
not be initiated because there are other users logged on to the computer.
At line:1 char:1
+ Restart-Computer -ComputerName PC01
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (PC01:String) [Restart-Computer], InvalidOperationException
    + FullyQualifiedErrorId : RestartcomputerFailed,Microsoft.PowerShell.Commands.RestartComputerCommand

That didn’t work. Why is that?

There are users logged in on the computer, and we can’t restart the remote computer. If there are no users logged in, a restart will immediately start without fail.

Restart remote computer with PowerShell Force

We have to use the -Force parameter to restart the computer. Doing that will immediately restart the computer without a countdown.

PS C:\> Restart-Computer -ComputerName "PC01" -Force

The computer is restarting.

Ping remote computer

After running the above cmdlet, ping the computer. It will show us if we can reach the computer.

PS C:\> ping PC01 -t

Reply from 192.168.1.100: bytes=32 time=9ms TTL=120
Reply from 192.168.1.100: bytes=32 time=9ms TTL=120
Reply from 192.168.1.100: bytes=32 time=10ms TTL=120
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Reply from 192.168.1.100: bytes=32 time=9ms TTL=120
Reply from 192.168.1.100: bytes=32 time=9ms TTL=120

The ping is showing a reply, request timed out, and reply of the remote computer:

  1. In the beginning, a reply is showing because the computer is still connected.
  2. After a couple of seconds, we can’t see a reply. We see a request timed out because the computer is offline.
  3. The computer is starting again, and a reply is showing.

The computer is back online, and we can access the computer with remote desktop.

It’s good to know how to restart computers remotely. The next time you want to restart a computer, make sure to use PowerShell. I hope that this article helped you.

Read more: Exchange setup can’t continue PowerShell has open files »

Conclusion

In this article, you learned how to restart remote computer with PowerShell. Run the Restart-Computer cmdlet as shown to restart the remote computer. You can always check if the computer is going offline and starting again by pinging the computer.

Did you like this article? If so, you may also like to read Enable Windows Firewall with PowerShell. Follow us on X and LinkedIn to stay up to date with the latest articles.

ALI TAJRAN is a passionate IT Architect, IT Consultant, and Microsoft MVP. He started Information Technology at a very young age, and his goal is to teach and inspire others. Read more »

Понравилась статья? Поделить с друзьями:
0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Старые
Новые Популярные
Межтекстовые Отзывы
Посмотреть все комментарии
  • Hkey local machine software wow6432node microsoft windows currentversion uninstall
  • Windows 10 пользователь для ребенка
  • Как обновить windows server 2012 r2 до windows server 2022
  • Как отключить майнер на компьютере windows 10
  • Как сделать скриншот выбранной области в windows 10