Public/Test-RebootRequired.ps1

<#
.SYNOPSIS
    Checks if a Windows system requires a reboot due to pending operations.
 
.DESCRIPTION
    This function checks various system components to determine if a reboot is pending.
    It checks the Component Based Servicing (CBS), Windows Update, pending file rename operations,
    and Configuration Manager (SCCM) Client for any pending reboot requirements.
 
.OUTPUTS
    [bool]
    Returns true if a reboot is pending, otherwise false.
 
.EXAMPLE
    Test-RebootRequired
    Checks if the system requires a reboot and returns a Boolean value.
    $params1 = @{
    ComputerName = "vdurjmp001"
    ScriptBlock = ${function:Test-RebootRequired}
    }
 
    Invoke-Command @params1
    Checks if the system requires a reboot on remote computer and returns a Boolean value.
 
.NOTES
    Author: Sundeep Eswarawaka
#>

function Test-RebootRequired {

    [CmdletBinding()]
    [OutputType([bool])]
    $result = @{
        CBSRebootPending = $false
        WindowsUpdateRebootRequired = $false
        FileRenamePending = $false
        SCCMRebootPending = $false
    }

    # Check Component Based Servicing registry for pending reboot
    if (Test-Path "HKLM:Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending") {
        $result.CBSRebootPending = $true
    }
    
    # Check Windows Update registry for reboot requirement
    if (Test-Path "HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired") {
        $result.WindowsUpdateRebootRequired = $true
    }
    
    # Check for pending file rename operations
    $prop = Get-ItemProperty "HKLM:SYSTEM\CurrentControlSet\Control\Session Manager" -Name PendingFileRenameOperations -ErrorAction SilentlyContinue
    if ($prop) {
        $result.FileRenamePending = $true
    }
    
    # Check SCCM Client for reboot pending
    try {
        $util = [wmiclass]"\\.\root\ccm\clientsdk:CCM_ClientUtilities"
        $status = $util.DetermineIfRebootPending()
        if ($status.RebootPending) {
            $result.SCCMRebootPending = $true
        }
    } catch {}

    #return $result.Values -contains $true
    return $result
}