Use PowerShell Objects to Simplify Script Output

Doctor Scripto

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to simplify creating script output.

Microsoft Scripting Guy, Ed Wilson, is here. All of the sessions for Windows PowerShell Saturday in Charlotte are pretty much set. The big question is whether we will have a really cool special guest, or a really, really cool special guest. Of course, you will never know the outcome. The tickets for the event have been going pretty quickly also. All of the previous Windows PowerShell Saturday events have sold out, and this one will be no exception. Last year, all the tickets sold out a couple of weeks before the event. This one may sell out even sooner. If you anticipate attending, sign in and get your ticket now. It is a pretty big deal. Last year, we had people from more than a dozen different states. It was a great time to get together, and talk about our favorite scripting language.

Now let’s talk about objects…

The easiest way to create an object is to pipe the object to the Select-Object cmdlet. This permits me to create a new object on the fly. In the following command, by using the Where syntax in Windows PowerShell 4.0, I find all the processes that are reporting CPU consumption and virtual memory consumption:

(Get-Process | select name, cpu, vm).where({$_.cpu -AND $_.vm})

Note  The cool thing about the Where syntax in Windows PowerShell 4.0 is that Where becomes a method on an object. I can therefore use same syntax I used when I piped to Where-Object, but the syntax is a bit more compact. The syntax for Windows PowerShell 3.0 and earlier is:

Get-Process | select name, cpu, vm | where {$_.cpu -AND $_.vm}

So I am returning a series of custom objects to the Windows PowerShell console. The command and the associated output are shown here:

Image of command output

Now I am pretty much done. I can add this command to a script, and my job as a scripter is complete. I have created a Windows PowerShell command that returns all processes that return CPU and virtual memory consumption information.

The formatting of that output is now left as an exercise to the person who needs to use the information. For example, do they want a list or a table? Do they want to sort by memory or by CPU utilization?

So, I put the command in a function, and I can call the function interactively via the Windows PowerShell ISE console. Here is the function (and while I am at it, I create an alias to make it easier to use):

# DemoObjectOutput.ps1

# ed wilson, msft

# hsg-11-14-13

# —————————————————————————–

#Requires -version 4.0

 

Function Get-ProcMemCPU

{

 (Get-Process | select name, cpu, vm).where({$_.cpu -AND $_.vm})

}

New-Alias -Name gpmc -Value Get-ProcMemCPU -Scope global

When I run the function, it is loaded into memory. So, I first sort by CPU utilization:

Get-ProcMemCPU | sort cpu

But what if the output needs to descend from largest to smallest? Well, that is a different command:

Get-ProcMemCPU | sort cpu -Descending

What if the output needs to be tightened up a bit? No problem, send the output to Format-Table and use –AutoSize:

Get-ProcMemCPU | sort vm -Descending | Format-Table -AutoSize

This command and its output are shown here:

Image of command output

Maybe the output should show memory consumed in gigabytes instead of bytes. I use the following command to format the columns in the Format-Table output:

Get-ProcMemCPU | sort vm -Descending | Format-Table Name, cpu, @{L=’Memory in Gig’;E={$_.vm /1GB}}-AutoSize

The command and the output are shown here:

Image of command output

I can even pipe the output to the Out-GridView cmdlet. If I use the –PassThru parameter, the process object I select returns to the Windows PowerShell ISE console line. If I continued the pipeline, I just added a GUI tool to my Windows PowerShell script. The following command permits me to select the process via Out-GridView and terminate the process via Stop-Process:

Get-ProcMemCPU | Out-GridView -PassThru | Stop-Process

The following image illustrates Out-GridView:

Image of command output

So by returning an object from my script, I permit a nearly infinite choice of additional actions—everything from formatting the output a special way to using the command as a basis of a GUI tool that permits me to kill the process with the greatest memory utilization.

That is all there is to using Windows PowerShell objects. Join me tomorrow when guest blogger, Marc Adam Carter, will share his reprised use of Windows PowerShell to find an installed software blog post. It will be really cool, and he has made some great improvements.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

0 comments

Discussion is closed.

Feedback usabilla icon