Skip to content

Restart-ComputerRemote

Windows: Restarts one or more local or remote computers

#Requires -Version 5.1

[CmdletBinding()]
Param
(
    [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
    [string[]]$ComputerName,

    [pscredential]$Credential,

    [switch]$Force,

    [switch]$Wait,

    [int]$Timeout = 300
)

Process
{
    try
    {
        $restartParams = @{
            'ComputerName' = $ComputerName
            'Force'        = $Force
            'Confirm'      = $false
            'ErrorAction'  = 'Stop'
        }

        if ($null -ne $Credential)
        {
            $restartParams.Add('Credential', $Credential)
        }

        if ($Wait)
        {
            $restartParams.Add('Wait', $true)
            $restartParams.Add('For', 'Wmi')
            $restartParams.Add('Timeout', $Timeout)
        }

        Write-Verbose "Initiating restart for: $($ComputerName -join ', ')"
        Restart-Computer @restartParams

        $result = [PSCustomObject]@{
            Computers = $ComputerName
            Action    = "Restart"
            Status    = if ($Wait) { "Completed (Available)" } else { "Initiated" }
            Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        }

        Write-Output $result
    }
    catch
    {
        throw
    }
}

Specifies one or more computer names or IP addresses to restart.

Specifies a PSCredential object for remote connection.

Off

Indicates that the restart should be forced even if users are logged on.

Off

Indicates that the script should wait for the computers to restart and become available before finishing.

Specifies the maximum time (in seconds) to wait for the computer to become available.

An interactive directory of PowerShell scripts.