To get the current information whether the machine is on power programmatically via powershell, you can use for example something like this:

(Get-WmiObject -Class BatteryStatus -Namespace root\wmi -ComputerName "localhost").PowerOnLine

However, from my testing on some Lenovo models I discovered that sometimes I get two objects returned by this command (like True, True, or True, False) – so you need to go through the array:

Function isOnPower {
    try{
        $power = (Get-WmiObject -Class BatteryStatus -Namespace root\wmi -ComputerName "localhost").PowerOnLine
        $final = $False
        Foreach ($p in $power)
        {
            if ($p -like "True"){$final = $True}
        }
        return $final
    } catch
        {return 1}
}

This seems to be working well so far.

Also, the whole class BatteryStatus provides even more information, so you might be able to work with even more detailed data:

(Get-WmiObject -Class BatteryStatus -Namespace root\wmi -ComputerName "localhost")