How to Restart Computer Using PowerShell

Problem

In this blog post, we will walk you through how to restart computer using PowerShell.

Using Restart-Computer

You can use Restart-Computer cmdlet in order to restart computer using PowerShell.


Restart-Computer

After entering this command, PowerShell will prompt you to confirm the restart. Type Y and press Enter to proceed with the restart.

If you want to force the restart without prompting for confirmation, you can add -Force parameter.


Restart-Computer -Force

Using Shutdown Command

We can also utilize CMD command like shutdown in order to restart computer since the commmands can be run on PowerShell.


shutdown /r /t 0

In this case we use shutdown command with /r option telling the computer to restart.

We also specify /t 0 option that sets the time delay before the shutdown to zero seconds, meaning the restart will occur immediately.

Using Windows API

To interact with Windows API, we will use [DllImport] attribute to import necessary functions. Specifically, we will use user32.dll to log off current user and also advapi32.dll to initiate system shutdown.


Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;

public static class WindowsAPI {
    [DllImport("user32.dll", SetLastError = true)]
    public static extern bool ExitWindowsEx(uint uFlags, uint dwReason);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool InitiateSystemShutdown(string lpMachineName, string lpMessage, uint dwTimeout, bool bForceAppsClosed, bool bRebootAfterShutdown);

    public const uint EWX_FORCE = 4;
    public const uint EWX_REBOOT = 2;
}
"@

# Log off current user (optional step, useful when running the script as a regular user)
[WindowsAPI]::ExitWindowsEx(0, 0)

# Initiate system shutdown to restart the computer
[WindowsAPI]::InitiateSystemShutdown($null, "Computer is restarting", 30, $true, $true)

The script will first log off current user (optional step, useful when running the script as a regular user). Then, it will initiate system shutdown to restart the computer.

The $true values passed as arguments indicate that all applications will be closed forcefully, and the system will reboot after shutdown.

This method directly uses Windows API and provides a more low-level approach to restart the computer.

Invoke C# Code to Restart Computer

Since PowerShell is deeply integrated with .NET Framework, we can call C# code from PowerShell. In this case, we invoke C# code from PowerShell that will restart the computer.


Add-Type -TypeDefinition @"
    using System.Diagnostics;

    public static class RestartComputer {
        public static void Restart() {
            Process.Start("shutdown", "/r /t 0");
        }
    }
"@
[RestartComputer]::Restart()

This code will create a C# class within PowerShell to execute the shutdown command with /r /t 0 options which restarts the computer immediately.

Conclusion

There are many solutions to restart computer using PowerShell. First, we can use Restart-Computer cmdlet. Second, we can also use CMD command like shutdown and specify necessary argument to restart the computer.

Third, we can also call Windows API function from PowerShell. Last, we can execute C# code that will restart the computer through PowerShell.