Check for a Battery

by Jul 1, 2010

If your script needs to know whether your computer has a battery, you can ask WMI. Here is a small function:

function Has-Battery {
@(Get-WmiObject Win32_Battery).Count -ne 0

if (@(Get-WmiObject Win32_Battery).Count -ne 0) {
} else {

Note the use of @() which wraps the result into an array so you can check the number of batteries. Most systems have only one, but there are some systems with more.