Monday, December 04, 2006

Oneliner

Todays script is a oneliner that sums up the memory consumption of alle running processes (or rather all visible running processes). It also demonstrates the use of ";" as a line separator

$a = get-process; foreach ($i in $a) {$sum = $sum + $i.workingset}; $sum / 1mb

1 comment:

Anonymous said...

BTW - you can pipe to foreach and it can take 3 scriptblocks. One gets called before any object, the middle one gets called for each object, the last one gets called after all objects are processed. Thus you can do this:
PS> gps |foreach {$sum=0} {$sum += $_.workingset} {[int]($sum/1mb)
1091

Enjoy
Jeffrey Snover [MSFT]
Windows PowerShell/MMC Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx