Public/OS/Power/Get-PowerMode.ps1

<#
Copyright © 2024 Integris. For internal company use only. All rights reserved.
#>


FUNCTION Get-PowerMode {
    <#
    .SYNOPSIS
    Retrieves and displays the current power mode of the system.
 
    .DESCRIPTION
    This function uses the PowerGetEffectiveOverlayScheme method from the powrprof.dll to determine the current power mode of the system. It identifies whether the system is set to "Best Power Efficiency," "Balanced," or "Best Performance."
 
    .EXAMPLE
    Get-PowerMode
 
    This command retrieves and displays the current power mode of the system.
 
    .NOTES
    This function requires the powrprof.dll library and may need to be run with appropriate permissions.
    #>


    [CmdletBinding()]
    PARAM ( )

    $function = @'
    [DllImport("powrprof.dll", EntryPoint="PowerGetEffectiveOverlayScheme")]
    public static extern int PowerGetEffectiveOverlayScheme(out Guid EffectiveOverlayGuid);
'@

    $power = Add-Type -MemberDefinition $function -Name "Power" -PassThru -Namespace System.Runtime.InteropServices

    $effectiveOverlayGuid = [Guid]::NewGuid()
    $ret = $power::PowerGetEffectiveOverlayScheme([ref]$effectiveOverlayGuid)

    if ($ret -eq 0) {
        IF ($effectiveOverlayGuid -eq "961cc777-2547-4f9d-8174-7d86181b8a7a") {
            $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{
                Hostname = $env:COMPUTERNAME
                PowerModeName = "Best Power Efficiency"
                PowerModeGUID = $effectiveOverlayGuid
            }
        }
        ELSEIF ($effectiveOverlayGuid -eq "00000000-0000-0000-0000-000000000000") {
            $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{
                Hostname = $env:COMPUTERNAME
                PowerModeName = "Balanced"
                PowerModeGUID = $effectiveOverlayGuid
            }
        }
        ELSEIF ($effectiveOverlayGuid -eq "ded574b5-45a0-4f42-8737-46345c09c238") {
            $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{
                Hostname = $env:COMPUTERNAME
                PowerModeName = "Best Performance"
                PowerModeGUID = $effectiveOverlayGuid
            }
        }
        ELSE {
            $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{
                Hostname = $env:COMPUTERNAME
                PowerModeName = "Unknown"
                PowerModeGUID = $effectiveOverlayGuid
            }
        }
    }
    ELSE {
        $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{
            Hostname = $env:COMPUTERNAME
            PowerModeName = "Unknown"
            PowerModeGUID = $effectiveOverlayGuid
        }
    }

    RETURN $Results | Select-Object Hostname, PowerModeName, PowerModeGUID
}