PowerShell – Out-GridView Headers (Using Get-Process)

PowerShell – Out-GridView Headers with Get-Process

I thought I would do a follow-up on a previous post about using out-GridView with Headers and use Get-Process this time.  Although this post is
similar to prior post, I’ve shortened a few things in regards to my collection of header information.

($A = Get-Process) | Out-GridView or OGV

Essentially the $A is creating a variable/collection of items and displays without column names.
Use $A.GetType() and as you see the object type is a collection(System.Array).

At this point $A returns the items that are in the collection including the headers that are automatically created and are the names that will be
used for our header object.

the $A and Get-Process with Out-Gridview/OGV both return the same results with out our extra header information. 

Discovering what makes up the header can be found by using the following commands.

The first item displays all the available Member Types available for $A.
$A | Get-member -MemberType *
Here is a mini sub-set of items that are displayed. I find it a tad strange that all items were not created as an AliasProperty.

The first 7 items identified as AliasProperty are used as the column headings from $A.
$A | Get-member -MemberType ‘AliasProperty’

$A | Get-member -MemberType ‘ScriptProperty’

Headings to be used from Get-member functions are:
Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id  SI ProcessName

Data to populate the tables arrives in the following form:
Handles AliasProperty Handles = Handlecount
Name AliasProperty Name = ProcessName
NPM AliasProperty NPM = NonpagedSystemMemorySize64
PM AliasProperty PM = PagedMemorySize64
SI AliasProperty SI = SessionId
VM AliasProperty VM = VirtualMemorySize64
WS AliasProperty WS = WorkingSet64
Id                         Property       int Id {get;}
CPU                   ScriptProperty System.Object CPU {get=$this.TotalProcessorTime.TotalSeconds;}

Running get-help get-process -full returns information about Get-Process.

This shows what the values are for column headings.  SessionID does not appear in the help file. ( there is a lot more information then what I’ve displayed)

Now that I’ve shown where the fields are for the headers that will be used for reporting lets build the Report-Header object.
Create an object to hold header values

$TestProperties = new-Object Object

By itself $TestProperties only returns the System.Object

Now lets setup the Properties we want in our object.
add-Member -inputobject $TestProperties -MemberType NoteProperty -Name “Handles” -Value “Handles
add-Member -inputobject $TestProperties -MemberType NoteProperty -Name “NPM” -Value “NPM(K)
add-Member -inputobject $TestProperties -MemberType NoteProperty -Name “PM” -Value “PM(K)
add-Member -InputObject $TestProperties -MemberType NoteProperty -Name “WS” -Value “WS(K)
add-Member -InputObject $TestProperties -MemberType NoteProperty -Name “VM” -Value “VM(K)
add-Member -InputObject $TestProperties -MemberType NoteProperty -Name “CPU” -Value “CPU(s)
add-Member -InputObject $TestProperties -MemberType NoteProperty -Name “Id” -Value “Id
add-Member -InputObject $TestProperties -MemberType NoteProperty -Name “SI” -Value “SI
add-Member -InputObject $TestProperties -MemberType NoteProperty -Name “ProcessName” -Value “ProcessName

I kept the values in the same order that Get-Process returns.

What I’m doing is creating an initialized array with values that will be used as the header of a report. For this example of working with Report-Headers I’ve used Get-Process to show that it’s pretty darn easy to quickly add report headers for use in OGV or Out-GridView functions.

At this point I have an collection/array that contains 1 initialized record of straight text that will be used for Out-GridView.

Without placing the statements into a function here is what you see when running $TestProperties | Out-GridView

There 1 initialized record showing headings. 🙂

Now that there is an initialized collection of header information we can combine $A and $TestProperties and generate our Out-GridView.

Ooops. 🙂 $TestProperties is not the type that allows for joining objects of data together.  You’ll need to create your collection/array to something

like this  $local:TestCollection = @() But it’s just as easy to create a simple function

function Report-Headers() {
# There are a few methods for createing objects. Keeping this simple
# to show the possibilities

# Sets up an empty object with header names for use in out-gridview
$local:TestCollection = @()

# Create a new object with the same values as in ($A = Get-Process) to hold header names
$TestProperties = new-Object Object
add-Member -inputobject $TestProperties -membertype NoteProperty -Name “Handles” -Value “Handles”
add-Member -inputobject $TestProperties -membertype NoteProperty -Name “NPM” -Value “NPM(K)”
add-Member -inputobject $TestProperties -membertype NoteProperty -Name “PM” -Value “PM(K)”
add-Member -InputObject $TestProperties -MemberType NoteProperty -Name “WS” -Value “WS(K)”
add-Member -InputObject $TestProperties -MemberType NoteProperty -Name “VM” -Value “VM(K)”
add-Member -InputObject $TestProperties -MemberType NoteProperty -Name “CPU” -Value “CPU(s)”
add-Member -InputObject $TestProperties -MemberType NoteProperty -Name “Id” -Value “Id”
add-Member -InputObject $TestProperties -MemberType NoteProperty -Name “SI” -Value “SI”
add-Member -InputObject $TestProperties -MemberType NoteProperty -Name “ProcessName” -Value “ProcessName”

$local:TestCollection += $TestProperties
$global:ReportHeaders = $local:TestCollection

I realize that there are similar ways to solve this issue.  However this is simple and works.

$Global:ReportHeaders | out-gridview

Please keep in mind that I’m not doing any calculations on the field values.  This is just a simple method for creating report headers if they are needed.

Well that’s it for this post.  Comments and suggestions are always welcome.




Categories: #kravis, #PowerShell, #programming, PowerShell, PowerShell Posts

1 reply

  1. Hi Joseph,

    thanks a ton for this elaborate update ;).
    I have a liking for well done documentations and learned by following along your lead.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


My Dark Life

Mes Mots

Poems, Travelogue, Writeups & more by Sazz!


From a dark home, To an empty soul, A hurt heart, Makes a writer gold.

Ushma Patel

When the green woods laugh with the voice of joy, And the dimpling stream runs laughing by; When the air does laugh with our merry wit, And the green hill laughs with the noise of it.

Leesa Love's Legacy

Spewing my Truth - Poetically


Second life fashion

Loly Hallison

"In moments of crisis, only the imagination is more important than the knowledge" Albert Einstein

%d bloggers like this: