13

Check Pending Reboot Status Using Powershell

In the world of system administration, you will eventually have a need to check if a server needs a reboot or simply check pending reboot status. After some scouring on the internet I’ve come up with a Powershell script to let me know if a server needs a reboot. The best part about it, is that it can all be done remotely. No need to RDP into multiple servers or computers and do it manually. Also, it will tell you an easy True or False if it needs a reboot. Let’s move on to the script.

If you have any questions feel fee to leave a comment and I’ll do my best to get back to you!

Remotely Check Pending Reboot Status Powershell Script


Function Get-PendingRebootStatus {
<#
.Synopsis
    This will check to see if a server or computer has a reboot pending.
    For updated help and examples refer to -Online version.
 
.NOTES
    Name: Get-PendingRebootStatus
    Author: theSysadminChannel
    Version: 1.2
    DateCreated: 2018-Jun-6
 
.LINK
    https://thesysadminchannel.com/remotely-check-pending-reboot-status-powershell -
 
 
.PARAMETER ComputerName
    By default it will check the local computer.
 
.EXAMPLE
    Get-PendingRebootStatus -ComputerName PAC-DC01, PAC-WIN1001
 
    Description:
    Check the computers PAC-DC01 and PAC-WIN1001 if there are any pending reboots.
#>
 
    [CmdletBinding()]
    Param (
        [Parameter(
            Mandatory = $false,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            Position=0
        )]
 
    [string[]]  $ComputerName = $env:COMPUTERNAME
    )
 
 
    BEGIN {}
 
    PROCESS {
        Foreach ($Computer in $ComputerName) {
            Try {
                $PendingReboot = $false
 
                $HKLM = [UInt32] "0x80000002"
                $WMI_Reg = [WMIClass] "\$Computer\root\default:StdRegProv"
 
                if ($WMI_Reg) {
                    if (($WMI_Reg.EnumKey($HKLM,"SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\")).sNames -contains 'RebootPending') {$PendingReboot = $true}
                    if (($WMI_Reg.EnumKey($HKLM,"SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\")).sNames -contains 'RebootRequired') {$PendingReboot = $true}
 
                    #Checking for SCCM namespace
                    $SCCM_Namespace = Get-WmiObject -Namespace ROOT\CCM\ClientSDK -List -ComputerName $Computer -ErrorAction Ignore
                    if ($SCCM_Namespace) {
                        if (([WmiClass]"\$Computer\ROOT\CCM\ClientSDK:CCM_ClientUtilities").DetermineIfRebootPending().RebootPending -eq $true) {$PendingReboot = $true}
                    }
 
                    [PSCustomObject]@{
                        ComputerName  = $Computer.ToUpper()
                        PendingReboot = $PendingReboot
                    }
                }
            } catch {
                Write-Error $_.Exception.Message
 
            } finally {
                #Clearing Variables
                $null = $WMI_Reg
                $null = $SCCM_Namespace
            }
        }
    }
 
    END {}
}

Get-PendingRebootStatus

How to run the Get-PendingRebootStatus script

In order to the run the script there are a couple of things you need to do.  First and foremost, you need to set your execution policy to RemoteSigned.  This is a standard with running any Powershell script.

Next you need to dot source the script since it is a function.  To dot source the script do the following:

  • Copy the script above and save it any location. In this example I’ll save it to my C:\_Scripts folder.
  • Within the Powershell Window type: . .\_Scripts\Get-PendingRebootStatus.ps1 – Note the two dots before the backslash.

Dot-Source Get-PendingRebootstatus
 

Hopefully this article has helped you check pending Reboot status for machines in your environment.  Let me know what you think. Also, consider subscribing to our Youtube Channel to get video demos and other related sysadmin content. While you’re at it, don’t forget to take a look at our other real world Powershell Scripts.

4.7/5 - (23 votes)

Paul Contreras

Hi, my name is Paul and I am a Sysadmin who enjoys working on various technologies from Microsoft, VMWare, Cisco and many others. Join me as I document my trials and tribulations of the daily grind of System Administration.

13 Comments

  1. Good Day, I tried this script and i did configure $Computers = Get-Content filename, I fixed the missing backslash on both lines from previous posts and Set-ExecutionPolicy as well. When I run the command line as –> PS C:\> ..\Scripts\Get-PendingRebootStatus.ps1 -Computername $Computer and the script does not fail but it does not yield any results it just goes back to PS C:\> which is the next line. I am not sure what i am doing wrong but are there cmdlets i need to load before running the script? any help would be great

    • Everyone you can down/install from an Elevated PowerShell window once I did this I was able to create a variable with a list of computers and this seems to work. this may not be right but it worked for me # Install the PendingReboot module from the PowerShell Gallery
      Install-Module -Name PendingReboot -Scope CurrentUser -Force -AllowClobber

  2. The shown code is missing a backslash in front of both $Computer vars “\”
    $WMI_Reg = [WMIClass] “\\$Computer\root\….”
    and here
    if ([WmiClass]”\\$Computer\ROOT\….

  3. wouldnt’ this be easier?
    !!(dir “HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending*”)

  4. I receive error get-pendingrebootstatus : Cannot convert value “\\W-1\root\default:StdRegProv” to type
    “System.Management.ManagementClass”. Error: “The RPC server is unavailable. (Exception from HRESULT:
    0x800706BA)”

  5. Just pointing out that there’s a typo in line 48. You need two \ before $Computer.

    So
    $WMI_Reg = [WMIClass] “\\$Computer\root\default:StdRegProv”

    Once I figured that out, it works like a champ. Thanks!

Leave a Reply

Your email address will not be published.