functions/Get-BatteryStatus.ps1
function Get-BatteryStatus { <# .SYNOPSIS Displays battery status to console .DESCRIPTION Displays battery status to console .EXAMPLE Get-BatteryStatus Get the battery status - showing current charge in percent and also of the battery is charging or discharging .EXAMPLE battery uses the alias to call this function #> [outputtype([system.string])] [alias('battery')] [CmdletBinding()] param ( ) process { $b = (Get-CimInstance -ClassName CIM_Battery) $Battery = [PSCustomObject]@{ IsCharging = if ($b.BatteryStatus -eq 1) { "Not Charging" } else { "Charging" } Charge = $b.EstimatedChargeRemaining #.GetValue(1) Remaining = $b.EstimatedRunTime #.GetValue(1) } $msg = "$($Battery.Charge)%" if ($Battery.IsCharging -eq "Charging") { $msg += " $($Battery.IsCharging) " } else { $msg += " / $($Battery.Remaining) mins - Discharging" } return $msg } } |