Public/OS/Power/Get-PowerSettings.ps1

FUNCTION Get-PowerSettings {

    [CmdletBinding()]
    PARAM ( )

    FUNCTION Get-ActionDescription {

        [CmdletBinding()]
        PARAM ( 
            [INT]$Value
        )

        switch ($value) {
            0 { return "Do Nothing" }
            1 { return "Sleep" }
            2 { return "Hibernate" }
            3 { return "Shutdown" }
            4 { return "Turn off the Display" }
            default { return "Unknown" }
        }
    }
    
    $ActivePlan = powercfg /getactivescheme | Out-String

    $ActivePlanGUID = If ($activePlan -match '([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})') {
        $matches[0]
    } else {
        throw "Unable to parse active power plan GUID"
        RETURN
    }

    $ActivePlanName = (& powercfg.exe /getactivescheme) -replace ".*\((.*)\).*", '$1'

    $MonitorAC = & powercfg.exe /query $ActivePlanGUID SUB_VIDEO VIDEOIDLE | Select-String "Current AC Power Setting"
    $MonitorDC = & powercfg.exe /query $ActivePlanGUID SUB_VIDEO VIDEOIDLE | Select-String "Current DC Power Setting"
    $SleepAC = & powercfg.exe /query $ActivePlanGUID SUB_SLEEP STANDBYIDLE | Select-String "Current AC Power Setting"
    $SleepDC = & powercfg.exe /query $ActivePlanGUID SUB_SLEEP STANDBYIDLE | Select-String "Current DC Power Setting"
    $PowerButtonAC = & powercfg.exe /query $ActivePlanGUID SUB_BUTTONS PBUTTONACTION | Select-String "Current AC Power Setting"
    $PowerButtonDC = & powercfg.exe /query $ActivePlanGUID SUB_BUTTONS PBUTTONACTION | Select-String "Current DC Power Setting"
    $SleepButtonAC = & powercfg.exe /query $ActivePlanGUID SUB_BUTTONS SBUTTONACTION | Select-String "Current AC Power Setting"
    $SleepButtonDC = & powercfg.exe /query $ActivePlanGUID SUB_BUTTONS SBUTTONACTION | Select-String "Current DC Power Setting"
    $LidCloseAC = & powercfg.exe /query $ActivePlanGUID SUB_BUTTONS LIDACTION | Select-String "Current AC Power Setting"
    $LidCloseDC = & powercfg.exe /query $ActivePlanGUID SUB_BUTTONS LIDACTION | Select-String "Current DC Power Setting"

    $MonitorAC -match '([0-9a-f]{1}x[0-9a-f]{8})' | Out-Null
    $MonitorACMinutes = [uint32]$Matches[0] / 60

    $MonitorDC -match '([0-9a-f]{1}x[0-9a-f]{8})' | Out-Null
    $MonitorDCMinutes = [uint32]$Matches[0] / 60

    $SleepAC -match '([0-9a-f]{1}x[0-9a-f]{8})' | Out-Null
    $SleepACMinutes = [uint32]$Matches[0] / 60

    $SleepDC -match '([0-9a-f]{1}x[0-9a-f]{8})' | Out-Null
    $SleepDCMinutes = [uint32]$Matches[0] / 60

    $PowerButtonAC -match '([0-9a-f]{1}x[0-9a-f]{8})' | Out-Null
    $PowerButtonACAction = Get-ActionDescription ([uint32]$Matches[0])

    $PowerButtonDC -match '([0-9a-f]{1}x[0-9a-f]{8})' | Out-Null
    $PowerButtonDCAction = Get-ActionDescription ([uint32]$Matches[0])

    $SleepButtonAC -match '([0-9a-f]{1}x[0-9a-f]{8})' | Out-Null
    $SleepButtonACAction = Get-ActionDescription ([uint32]$Matches[0])

    $SleepButtonDC -match '([0-9a-f]{1}x[0-9a-f]{8})' | Out-Null
    $SleepButtonDCAction = Get-ActionDescription ([uint32]$Matches[0])

    $LidCloseAC -match '([0-9a-f]{1}x[0-9a-f]{8})' | Out-Null
    $LidCloseACAction = Get-ActionDescription ([uint32]$Matches[0])

    $LidCloseDC -match '([0-9a-f]{1}x[0-9a-f]{8})' | Out-Null
    $LidCloseDCAction = Get-ActionDescription ([uint32]$Matches[0])

    $Results += [PSCustomObject]@{
        PlanName = $ActivePlanName
        PlanGUID = $ActivePlanGUID
        MonitorTimeoutAC = $MonitorACMinutes
        SleepTimeoutAC = $SleepACMinutes
        PowerButtonActionAC = $PowerButtonACAction
        SleepButtonActionAC = $SleepButtonACAction
        LidCloseActionAC = $LidCloseACAction
        MonitorTimeoutDC = $MonitorDCMinutes        
        SleepTimeoutDC = $SleepDCMinutes        
        PowerButtonActionDC = $PowerButtonDCAction        
        SleepButtonActionDC = $SleepButtonDCAction        
        LidCloseActionDC = $LidCloseDCAction
    }

    RETURN $Results
}