Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Process Snapshot

Posted on May 11, 2011May 11, 2011

Yesterday I ended up running an impromptu browser test, comparing memory utilization. See what Twitter can do to your time!! The browsers themselves are really irrelevant. What you might find useful is the little PowerShell code I put together to periodically check and compare the browser processes.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

I decided to watch the WorkingSet property as a calculation of memory utilization. The value is in bytes. For example, here's the current workingset size for my PowerShell session.

[cc lang="DOS"]
PS S:\> (get-process -id $pid).workingset
236036096
[/cc]

I can easily convert this to MB.

[cc lang="DOS"]
PS S:\> (get-process -id $pid).workingset/1mb
225.390625
[/cc]

I can do the same for any process. The browsers I tested were Firefox 4, Internet Explorer 9 and the latest version of Chrome. All had the same 4 sites opened and were pretty much left untouched. To calculate the memory usage I can do the same for each process. But there is one potential challenge. Firefox runs everything in a single process so I could use the same technique. However, the other two browsers run multiple instances of the same process. So what I have to do is add up the working set property for all the processes and then divide by 1MB. I can do this in one line.

[cc lang="DOS"]
PS S:\> (get-process chrome | measure-object ws -sum).sum/1mb
369.71875
[/cc]

All of the Chrome processes are piped to Measure-Object which calculates the sum on the ws property (which is an alias for Workingset). That returns a measurement object which I "get" by wrapping the expression in parentheses. I can then get the sum property value and divide by 1MB. That's the core comparison. But I had to do that for each browser process. So I put together a short script to make this more meaningful and easier to run.

[cc lang="PowerShell"]
#Requires -version 2.0

#array of process names
$Procs="iexplore","chrome","firefox"

$Procs | foreach {
#get the earliest start time
$started=Get-Process $_ | sort StartTime | Select -first 1
#create a custom object
New-Object PSObject -Property @{
Name=$_
WS=(get-process $_ | measure-object ws -sum).sum/1mb
Report=Get-Date
Started=$started.StartTime
Elapsed=(Get-Date)-($Started.StartTime)
}
} #foreach
[/cc]

The script takes an array of process names and pipes them to ForEach-Object. Each process gets measured. I also get when the process started. For browsers with multiple processes, I get the process with the earliest starttime. With this I can calculate a run time. These values are assembled into a new object and written to the pipeline. Here's the end result.

[cc lang="DOS"]
PS S:\> C:\work\browsertest.ps1 | sort ws

Name : firefox
Started : 5/10/2011 12:20:13 PM
WS : 252.93359375
Elapsed : 20:14:02.8323717
Report : 5/11/2011 8:34:16 AM

Name : iexplore
Started : 5/10/2011 6:09:57 PM
WS : 314.6015625
Elapsed : 14:24:19.1541740
Report : 5/11/2011 8:34:16 AM

Name : chrome
Started : 5/10/2011 12:19:43 PM
WS : 369.12109375
Elapsed : 20:14:33.1991086
Report : 5/11/2011 8:34:16 AM
[/cc]

By creating objects I now have opportunity. For example, I could periodically run this command and export the results to a CSV file which I could then open in Microsoft Excel and analyze or chart. You could easily turn this into a function specifying any process names and even remote computer names. Although don't forget to add some error handling for non-existent processes.

If you want to play with this yourself, download the code.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

1 thought on “Process Snapshot”

  1. Vincent says:
    May 12, 2011 at 3:20 am

    Yay firefox ! 🙂
    Neat script.

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d