Use a script block to create custom groupings in PowerShell

Doctor Scripto

Summary: Learn how to use a script block with the Group-Object cmdlet in Windows PowerShell to create custom groupings in this article by Microsoft Scripting Guy Ed Wilson.

Good morning. Ed Wilson the Microsoft Scripting Guy is here. This week, I have been hanging out with a group of writers. It has been both fun and educational. It is always good to see how other people solve standard types of problems. As I look around, I can see nearly a dozen other writers huddled into little conversation groups as they discuss problems of craft.

Anyway, with Windows PowerShell, there are also standard problems of craft with which we need to work. Standard problems often demand standard answers. We often call these patterns and practices. Sometimes, of course, there are unique answers to a standard problem. We often call these elegant solutions. And then, there are the things that we sometimes discover that mighg have been around for a long time, and maybe we forgot about them, or we simply overlooked them. One of those things is using a script block with the Group-Object cmdlet.

Group with a script block

A script block in Windows PowerShell falls within a set of curly braces {}. Many parameters in Windows PowerShell can accept a script block. I can find this by using Get-Command and specifying a -ParameterType of scriptblock. This command and output is shown here:

Screenshot that show output from Get-Command -ParameterType scriptblock.

What’s interesting is that the Group-Object cmdlet doesn’t appear in this list. And when I look at the syntax of Group-Object, I also don’t see a scriptblock mentioned anywhere. This is the syntax for the Group-Object cmdlet.

PS C:\> Get-Command Group-Object -Syntax

Group-Object [[-Property] <Object[]>] [-NoElement] [-AsHashTable] [-AsString]

[-InputObject <psobject>] [-Culture <string>] [-CaseSensitive] [<CommonParameters>]

Using a script block with Group-Object is not exactly undocumented. It is, after all, mentioned in an example from the cmdlet help. This is shown here:

Screenshot of an example that mentions script block in Group-Object help.

Explore the possibilities of grouping with a script block

One thing that’s interesting is when you group by one element and then calculate a second element that is also used for the second sort. The command that appears here gets, processes, and groups by name. That is, all processes with the same name are grouped together. But in addition, if the name matches conhost, it will display True.

Get-Process | Group-Object name, { $_.name -match '^con'}

The command and output is shown here:

Screenshot that shows results from Get-Process | Group-Object name, { $_.name -match '^con'}.

Group names and report memory

One thing that is pretty cool is to group processes by name and then calculate the working set of each process in megabytes (MB). The command is shown here:

gps | Group-Object name, {[int]($_.WorkingSet / 1MB)} -NoElement | sort name

While technically, the second property in Group-Object is a working set, few processes with the same name will have the same working set. But it does provide additional information that is not normally available when you use Group-Object. The output is shown here:

Screenshot of results that group processes by name and then calculate the working set of each process in megabytes.

Join me tomorrow when I will play around with some more interesting features of the Group-Object cmdlet.

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. Also check out my Microsoft Operations Management Suite Blog. See you tomorrow. Until then, peace.

Ed Wilson Microsoft Scripting Guy

0 comments

Discussion is closed.

Feedback usabilla icon