title: WMI BatteryStatus from Windows Server 2012 using Powershell
body: <p>I'm writing a Powershell script to do some server management when we have a power outage. Presently the script is using the WMI Win32)Battery class to see if BatteryStatus is a 1 (discharging) or a 2 (unknown (on A/C)) but I am not sure this is the best method. There are several statuses that could be true at the same time.</p>
  
  <p>For example, the battery could be Discharging (1) and Partially Charged (11) or On AC (2) but Critical (5) or charging (6) or Partially Charged (11). This doesn't make sense to me. Is BatteryStatus reliable for my intended purpose? How does Windows determine which status is returned? In my testing I've not seen anything other than statuses of 1 or 2.</p>
  
  <p>The documentation for <a href="http://www.powertheshell.com/reference/wmireference/root/cimv2/Win32_Battery/" rel="nofollow noreferrer">Win32_Battery</a> indicates Availability might also be an indicator that we are on battery. Is that a better way to go, maybe a combination of BatteryStatus and Availability?</p>
  
  <p>In my search for answers I came across what looks like <a href="https://blogs.technet.microsoft.com/heyscriptingguy/2010/07/31/using-windows-powershell-to-determine-if-a-laptop-is-on-battery-power/" rel="nofollow noreferrer">the perfect answer</a>, using "PowerOnline" from the undocumented BatteryStatus class in the root\wmi namespace. This works great from my laptop but I get the following error when trying to access it from our Windows 2012 server:</p>
  
  <pre><code>PS C:\Windows\system32&gt; gwmi -Class batterystatus -Namespace root\wmi 
  gwmi : Invalid class "batterystatus"
  At line:1 char:1
  + gwmi -Class batterystatus -Namespace root\wmi
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidType: (:) [Get-WmiObject], ManagementException
      + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
  </code></pre>
  
  <p>Is this class not available on servers?</p>
  
  <p>So using WMI, what is the most reliable way to know if I am on UPS battery or AC Power? </p>