PowerShell/Scripts/check-battery.ps1

35 lines
1.0 KiB
PowerShell
Raw Normal View History

2022-11-30 08:42:00 +01:00
<#
.SYNOPSIS
Checks the battery status
.DESCRIPTION
This PowerShell script checks and prints the battery status.
.EXAMPLE
PS> ./check-battery
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
try {
if ($IsLinux) {
# TODO
} else {
Add-Type -Assembly System.Windows.Forms
$Details = [System.Windows.Forms.SystemInformation]::PowerStatus
switch ($Details.BatteryChargeStatus) {
2022-12-01 20:54:52 +01:00
"NoSystemBattery" { $BatteryStatus = "No battery" }
"0" { [int]$Life = 100*$Details.BatteryLifePercent; [int]$Remaining = $Details.BatteryLifeRemaining / 60; $BatteryStatus = "Battery $Life%, $Remaining min left" }
default { $BatteryStatus = $Details.BatteryChargeStatus }
}
2022-12-01 20:54:52 +01:00
switch ($Details.PowerLineStatus) {
"Online" { $PowerStatus = "plugged in to AC power" }
"Offline" { $PowerStatus = "disconnected from AC power" }
}
"$BatteryStatus, $PowerStatus"
2022-11-30 08:42:00 +01:00
}
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}