Introduction

Ensuring that your UPS (Uninterruptible Power Supply) is correctly connected and monitored is essential for protecting your systems from power failures. In this guide, we will walk through various methods to check the status of your UPS using PowerShell, including checking with WMI, CIM, and automating notifications for low battery levels.


1. Checking if a UPS is Connected via WMI and CIM

Many UPS devices, especially those connected via USB, are detected as battery devices on Windows systems. We can use WMI and CIM to extract information about these devices.

A. Using WMI

Example:


# Get information about connected UPS or battery devices
$upsInfo = Get-WmiObject -Class Win32_Battery

# Display details if a UPS is detected
if ($upsInfo) {
    $upsInfo | Format-Table -Property Name, BatteryStatus, EstimatedChargeRemaining, EstimatedRunTime
} else {
    Write-Host "No UPS or battery device detected."
}

Explanation: This script checks if your UPS is recognized as a battery device. It shows the current battery status, remaining charge percentage, and estimated runtime in minutes.


B. Using CIM

If you are using a newer version of Windows, consider using the CIM cmdlet instead:


# Get UPS information using CIM
$upsInfoCim = Get-CimInstance -ClassName Win32_Battery

if ($upsInfoCim) {
    $upsInfoCim | Select-Object Name, BatteryStatus, EstimatedChargeRemaining, EstimatedRunTime
} else {
    Write-Host "No UPS or battery device detected via CIM."
}

How to Try It: Open a PowerShell window, copy and paste the script, and check if your UPS is properly detected.


2. Monitoring Power Events and Power Source Changes

It is useful to monitor power status changes, especially if you need to be alerted when your system switches from AC power to the UPS battery.


# Monitor power status changes
$powerStatus = Get-WmiObject -Class Win32_PowerManagementEvent
switch ($powerStatus.PowerState) {
    4 { Write-Host "The system is running on AC power." }
    5 { Write-Host "The system is running on UPS battery power." }
    default { Write-Host "Unknown power state." }
}

Explanation: This script helps you identify whether your system is currently powered by AC or UPS battery.


3. Using Built-in PowerShell Tools to Get Device Information

If the UPS is not recognized under the Win32_Battery class, you can check for connected devices using the Get-PnpDevice cmdlet:


# List all battery-related devices, including UPS
Get-PnpDevice -Class Battery | Format-Table -Property Name, Status, DeviceID

How to Try It: This command lists all devices detected as batteries. If your UPS is listed, it is properly connected.


Check for Devices Under Power Devices


# Check for UPS devices under Power class
Get-PnpDevice -Class "Power" | Format-Table -Property Name, Status, DeviceID

This command helps identify if the UPS is detected under the “Power” device class.


4. Automating UPS Monitoring for Notifications and Logging

To create a more robust solution, set up a PowerShell script that continuously monitors the UPS status and sends notifications if the battery level drops below a certain threshold.


# Define log file location
$logFile = "C:\UPS_Monitoring\ups_status_log.txt"
mkdir -Force (Split-Path $logFile)

# Function to check UPS status
function Check-UPSStatus {
    $ups = Get-CimInstance -ClassName Win32_Battery
    if ($ups) {
        $status = $ups.BatteryStatus
        $charge = $ups.EstimatedChargeRemaining
        $runTime = $ups.EstimatedRunTime

        $message = "UPS Status: $status, Charge: $charge%, Estimated Run Time: $runTime minutes"
        Write-Host $message

        # Log to file
        $timestamp = Get-Date -Format "yyyy-MM-dd HHmmss"
        "$timestamp - $message" | Out-File -FilePath $logFile -Append

        # Notify if battery is below a certain threshold
        if ($charge -lt 20) {
            [System.Windows.Forms.MessageBox]::Show("Warning: UPS battery is below 20%", "UPS Alert")
        }
    } else {
        Write-Host "No UPS detected"
    }
}

# Schedule to run every 5 minutes
while ($true) {
    Check-UPSStatus
    Start-Sleep -Seconds 300
}

How to Try It: Copy the script into a PowerShell window and run it. This script will monitor the UPS status and send notifications if the battery charge is low.


Conclusion

By using PowerShell scripts, you can effectively monitor your UPS, check its status, and automate alerts for critical events. This approach is flexible, allowing integration with other monitoring systems or centralized logging platforms.

 


References