Selecting Objects with PowerShell


I think by nature most people are picky. In PowerShell, this is actually a positive attribute. PowerShell is designed to give you a lot of information. Sometimes more than you really need. PowerShell is also designed to display command results that someone from Microsoft, or a vendor in the world of third-party cmdlets, thought you would most likely want to see or use. Of course, you know that there is always much more to the picture than what you see. Once you discover what you can use, you can afford to be a bit pickier and the cmdlet that makes this easy is Select-Object. If you are new to using PowerShell objects, read Introduction to Objects in PowerShell on Petri IT Knowledgebase.

There are a few ways to use Select-Object. Perhaps the most important way is to use it to display a subset of object properties. You should know that properties you see when you run a command like Get-Process are not the only properties. In some cases, what you see isn’t even the actual property name. The best way is to use Get-Member to discover the actual property names.

Viewing object properties with Get-Member
Viewing object properties with Get-Member (Image Credit: Jeff Hicks)

Once you know the property names, you can run the command and pipe it to Select-Object specifying the property names you want to see.

Sponsored Content

What is “Inside Microsoft Teams”?

“Inside Microsoft Teams” is a webcast series, now in Season 4 for IT pros hosted by Microsoft Product Manager, Stephen Rose. Stephen & his guests comprised of customers, partners, and real-world experts share best practices of planning, deploying, adopting, managing, and securing Teams. You can watch any episode at your convenience, find resources, blogs, reviews of accessories certified for Teams, bonus clips, and information regarding upcoming live broadcasts. Our next episode, “Polaris Inc., and Microsoft Teams- Reinventing how we work and play” will be airing on Oct. 28th from 10-11am PST.

Selecting specific process properties
Selecting specific process properties (Image Credit: Jeff Hicks)

When you use Select-Object, you get the raw property value. In some cases, such as with Get-Process, PowerShell has special rules to format the output. You may need to do something similar. For example, the WorkingSet property (using the WS alias) is being displayed in bytes. Perhaps I need to see that value in KB. Using a specific hashtable structure, I can define a custom property. The hashtable has two keys, Name and Expression. The value for Name is whatever you want to call your property. The value for Expression is a scriptblock. PowerShell will evaluate the scriptblock and use the result as the output. You can use as much PowerShell code as you need. If you want to reference the current object, use the $_ construct.

get-process | Select ID,Name,@{Name="WS(KB)"; Expression = { $_.WS/1kb}},Path

The hashtable will define a property called WS(KB), which will have a value of the current WS value divided by 1KB.

Using a custom property
Using a custom property (Image Credit: Jeff Hicks)

I love using this hashtable trick because you can create entirely new properties to fit your needs.

dir c:\work -file | Select Name,@{Name="Size";Expression={$_.length}},
CreationTime,LastWriteTime,@{Name="ModifiedAge";Expression={(Get-Date) - $_.lastwritetime}} |
sort ModifiedAge -Descending | Out-Gridview -Title "Work Files"

I created a few custom properties here. The first is essentially an alias property for file length that I’m calling Size, which I find more meaningful. I also created a new property called ModifiedAge that calculates a timespan indicating how long it has been since the file was modified. When you create these types of custom properties with Select-Object, they are passed on in the pipeline, which means you use can use them with other commands such as Sort-Object. That is exactly what I am doing here. I’m sorting on the new ModifiedAge property and piping the results to Out-Gridview.

Using a custom property in the pipeline
Using a custom property in the pipeline (Image Credit: Jeff Hicks)

This brings me to the other way you are likely to use Select-Object.

Sometimes you only want the first or last X number of objects. For instance, using my previous example, perhaps I only want to see the 10 oldest files. Because they are sorted in descending order, I can use the -First parameter with Select-Object.

CreationTime,LastWriteTime,@{Name="ModifiedAge";Expression={(Get-Date) - $_.lastwritetime}} |
sort ModifiedAge -Descending | Select -first 10 | format-table

There’s nothing wrong with using Select-Object twice. In this case, I have to because I need to define my custom property and then sort on it before I can select the first 10 objects.

Selecting first 10 objects
Selecting first 10 objects (Image Credit: Jeff Hicks)

Although you can certainly select key properties and specify first or last in the same command.

Selecting properties and a subset of objects
Selecting properties and a subset of objects (Image Credit: Jeff Hicks)

It all depends on what you need to accomplish.

There are a few other parameters you might use with Select-Object, so be sure to take a few minutes to read complete help and examples. But I think the majority of your work with Select-Object will be using the techniques I’ve demonstrated here. I think Select-Object is one of the cmdlets that you will use constantly, so take some time to learn how to use it to your advantage.

If you want to learn about filtering with PowerShell, read Filtering PowerShell on Petri.

Related Topics:


Don't have a login but want to join the conversation? Sign up for a Petri Account

Comments (0)

Leave a Reply

External Sharing and Guest User Access in Microsoft 365 and Teams

This eBook will dive into policy considerations you need to make when creating and managing guest user access to your Teams network, as well as the different layers of guest access and the common challenges that accompany a more complicated Microsoft 365 infrastructure.

You will learn:

  • Who should be allowed to be invited as a guest?
  • What type of guests should be able to access files in SharePoint and OneDrive?
  • How should guests be offboarded?
  • How should you determine who has access to sensitive information in your environment?

Sponsored by:

Live Webinar: Active Directory Security: What Needs Immediate Priority!Live on Tuesday, October 12th at 1 PM ET

Attacks on Active Directory are at an all-time high. Companies that are not taking heed are being punished, both monetarily and with loss of production.

In this webinar, you will learn:

  • How to prioritize vulnerability management
  • What attackers are leveraging to breach organizations
  • Where Active Directory security needs immediate attention
  • Overall strategy to secure your environment and keep it secured

Sponsored by: