Idera nSoftware Compellent

PowerShell.com

Join PowerShell.com!
PowerShell eBook
Script Library
Video Library
Meet Dr. Tobias Weltner


PowerShell eBook
Sign up for
Your PowerTip of the Day:

PowerShellPlus


Practical PowerShell Video Series


Live Chat


Twitters


Membership Privileges


Monthly Membership Prizes

Winner Sylvain Lesire (Mar.)
Niklas Goude (Feb.)
Sylvain Lesire (Jan.)
Aleksandar Nikolic (Dec.)
Thomas Lee (Nov.)

Welcome to PowerShell.com, the educational and community site for Windows PowerShell People. Get a quick overview.

Latest Blog Posts

07-03-2009 - Summing Up Multiple Objects
07-02-2009 - PowerScripting Podcast update
07-02-2009 - Adding Multiple Registry Keys

Latest News from Tobias

05-25-2009 - Feeding Input into Native Commands
05-01-2009 - PowerShellPlus 3.0 released!
04-23-2009 - PowerShell Roadshow in Germany

Tobias' Tip of the Day               All PowerTips

Summing Up Multiple Objects

PowerShell is a dynamic language, and as Forest Gump put it, you never know what you get. For example, when you try and figure out the battery charge on a notebook, the following command may return null, one or multiple results depending on the number of batteries in your system:

Get-WMIObject Win32_Battery

To find out the overall charge of all batteries in your system, simply use Measure-Object. Measure-Object happily accepts an arbitrary number of (equal) objects and aggregates information. To find out the overall charge remaining in all of your batteries, use this:

Get-WMIObject Win32_Battery | Measure-Object EstimatedChargeRemaining -average
(Get-WMIObject Win32_Battery |
Measure-Object EstimatedChargeRemaining -average).Average

Note that this call will fail if there is no battery at all in your system because then Measure-Object is unable to find a EstimatedchargeRemaining property.

Copyright 2008 PowerShell.com. All rights reserved.