Calling WinRT Directly from PowerShell: Observing Windows Power States with PowerManager

PowerShellカテゴリを表すパンダのイラスト PowerShell

About This Article
This article was created using an automated generation workflow leveraging generative AI. It re-verifies the official Windows Runtime PowerManager API alongside existing Daily-Code-Samples, organizing them as a minimal smoke test for reading power states from PowerShell.

Verification Status: 📘 Official Information Confirmed / Windows Physical Device Not Verified

Calling WinRT directly from PowerShell gives you the experience of reading Windows power states "from the OS API rather than via cmdlets." The success condition for this test is[SUCCESS] along with RemainingChargePercent and other values being displayed at least once.

Getting Started

The target environment is Windows PowerShell 5.1 + Windows 10/11. Because this only reads values, administrator privileges are not required.

$pm = [Windows.System.Power.PowerManager, Windows.System.Power, ContentType = WindowsRuntime]

Write-Host '[START] PowerManager を読みます'
try {
    $result = [pscustomobject]@{
        RemainingChargePercent = $pm::RemainingChargePercent
        BatteryStatus          = [string]$pm::BatteryStatus
        PowerSupplyStatus      = [string]$pm::PowerSupplyStatus
        EnergySaverStatus      = [string]$pm::EnergySaverStatus
    }

    Write-Host '[SUCCESS] PowerManager を読み取れました'
    $result | Format-List
}
catch {
    Write-Host '[FAILED] PowerManager の読み取りに失敗しました'
    Write-Host ("Type    : {0}" -f $_.Exception.GetType().FullName)
    Write-Host ("Message : {0}" -f $_.Exception.Message)
}

Properties exposed by PowerManager can be checked on Microsoft Learn.

What to Look For

On a laptop, RemainingChargePercentBatteryStatusPowerSupplyStatus is an easy value to observe. In environments without a battery, such as a desktop PC, the values may not match those of a laptop.

The important point, more than the values themselves, is that PowerShell is traversing the following layer.

flowchart LR
    PS[Windows PowerShell 5.1] --> RT[WinRT 型解決]
    RT --> PM[Windows.System.Power.PowerManager]
    PM --> OS[Windows の電源状態]
    OS --> PM --> PS

ContentType = WindowsRuntime is a directive used to "resolve this type as a Windows Runtime type." You are not simply calling a standard PowerShell cmdlet.

Trying One Change

If you are safely testing on a laptop, keep the code unchanged and modify only the AC adapter connection state before running it again.BatteryStatus Observe how PowerSupplyStatus and

change. The key here is not to assume that the values will always be a specific fixed number. Results will vary depending on the hardware, drivers, and power state.

Why Access WinRT from PowerShell?

WinRT is one of the API sets exposed by Windows for applications. For APIs whose types can be directly resolved from PowerShell, it allows you to observe in a small-scale way "what kind of API underpins Windows settings and states beneath the GUI."

While the entry point differs from calling Win32 APIs via Declare in VBA, the fundamental concept of "using APIs exposed by the OS from another language" remains the same.

For Production Use

Rather than using this minimal example as-is for a monitoring system, you should add the following considerations:

  • Record the PowerShell and Windows version

  • Account for PCs without batteries

  • Do not treat retrieval failures as successes

  • Avoid outputting unnecessary device information to logs

  • Do not assume the exact same code will run on PowerShell 7

Since this is a smoke test, the primary focus is simply calling the API once and verifying whether a value can be successfully retrieved.

GitHub Sample

The reusable version includes success/failure reporting and additional properties.

Official / Primary Sources

Conclusion

Calling WinRT from PowerShell allows you to observe Windows power states directly from the entry point of the OS APIs. By starting with the simple goal of "success if a single value is retrieved," and then changing just one condition such as the AC connection state, it becomes much easier to trace the connections between APIs, states, and outputs.

Document information

Article title
Calling WinRT Directly from PowerShell: Observing Windows Power States with PowerManager
Published
Updated
Source
https://papanda925.com/?p=15330&lang=en

License: Text and original figures for which this site holds the relevant rights are available under CC BY 4.0 , unless otherwise noted. This article may include content created or edited with generative AI. If code has a separate license notice or a linked GitHub repository license, that license takes precedence for the code. Quotations, third-party materials, images, and trademarks are excluded from this license. Usage policy

Copied title and URL