Thursday, December 12, 2024

Programmatically Determine if On Battery or Plugged In via Powershell

function onBattery { 
    if ((Get-WmiObject -Class Win32_Battery | Select-Object -ExpandProperty batterystatus) -eq 2) {
        write-output "Plugged In" 
    }
    else { 
        write-output "On Battery" 
    }
}