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

Get Process Detail

Posted on May 12, 2011

The other day I posted a snippet of code that I as using to monitor process memory utilization for a few web browsers. I thought the information and technique were useful enough to modularize in the form of a function. Perhaps I want to check working set on a different process or even on a different computer. This is what I came up with.

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 created a function called Get-ProcessWS. That's awkard to type so you might consider adding an alias like gpws.

[cc lang="PowerShell"]
Function Get-ProcessWS {

[cmdletbinding()]
Param (
[Parameter(Position=0,Mandatory=$True,HelpMessage="Enter a process name",
ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
[string[]]$Name,
[ValidateNotNullOrEmpty()]
[string]$Computername=$env:Computername
)

Begin {
Write-Verbose "$(Get-Date) Starting $($myinvocation.command)"
} #begin

Process {
Foreach ($item in $Name) {
Write-Verbose "$(Get-Date) Checking $Item on $Computername"

#see if the process is running
$process=Get-Process -Name $Item -ComputerName $Computername -ea "silentlycontinue"

#only process if we got a process back
if ($process)
{
#get the earliest start time
$started= $process | Sort-Object -property StartTime | Select-Object -first 1
$measure=$process | Measure-Object -Property workingset -sum
#divide by 1MB
$ws=$measure.sum/1mb
#count the number of processes
$count=($process | Measure-Object).Count
#create a custom object
New-Object -TypeName PSObject -Property @{
Name=$Item
WorkingSet=$ws
Report=Get-Date
Started=$started.StartTime
Elapsed=(Get-Date)-($Started.StartTime)
Computername=$Computername
Count=$Count
}
} #if
else
{
Write-Verbose "$(Get-Date) Process not found"
}
} #foreach
} #Process

End {
Write-Verbose "$(Get-Date) Ending $($myinvocation.command)"
} #end

} #end function
[/cc]

The function takes one or more process names and attempts to retrieve those processes on the specified computer. The default is the localhost.

[cc lang="PowerShell"]
$process=Get-Process -Name $Item -ComputerName $Computername -ea "silentlycontinue"
[/cc]

I'm suppressing any exceptions that will occur if the process is not running. Assuming the process is found, the function retrieves and calculates some values.

[cc lang="PowerShell"]
if ($process)
{
#get the earliest start time
$started= $process | Sort-Object -property StartTime | Select-Object -first 1
$measure=$process | Measure-Object -Property workingset -sum
#divide by 1MB
$ws=$measure.sum/1mb
#count the number of processes
$count=($process | Measure-Object).Count
[/cc]

The function then writes a custom object to the pipeline. I've added a few items from my original code such as the computer name and the number of processes.

[cc lang="PowerShell"]
#create a custom object
New-Object -TypeName PSObject -Property @{
Name=$Item
WorkingSet=$ws
Report=Get-Date
Started=$started.StartTime
Elapsed=(Get-Date)-($Started.StartTime)
Computername=$Computername
Count=$Count
}
[/cc]

The end result is an object like this:

[cc lang="DOS"]
Name : chrome
WorkingSet : 284.33203125
Elapsed : 14:18:14.5739805
Report : 5/12/2011 8:29:27 AM
Computername : SERENITY
Count : 9
Started : 5/11/2011 6:11:12 PM
[/cc]

You can pass process names as an array or pipe names to the function. The function writes to the pipeline so you can sort,convert, export, save to a file or whatever.

Download Get-ProcessWS.


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

2 thoughts on “Get Process Detail”

  1. jv says:
    May 12, 2011 at 6:42 pm

    Type
    Get-ProcessWS ie*
    or any other two char and a * that matches at least the first two chars of process name.

    Try sv* as it will always exist. It should return either nothing or a list of matching proesses but it return only the first one found.

    Why?

    1. Jeffery Hicks says:
      May 12, 2011 at 7:25 pm

      If Get-Process ie* returns something, then my function will process it. Which makes me realize I should tweak the function to return the name from the process object. Regardless, I’m not sure I follow your question. If I run my function for sv* I get the same 14 processes that I get running Get-Process sv*

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