PowerShell: Getting Properties From Objects In Arrays

In PowerShell, I often run commands that return arrays I need to filter. Once I’ve filtered out object I’m looking for, I need to read a property off that object. There are a few ways to do this. Here are three.

These are also good examples if you’re new to PowerShell and trying to switch from the Linux mindset of parsing strings to the PowerShell mindset of manipulating objects.

For these examples we’ll be using an array filtered from Get-Process:

Get-Process | Where-Object ProcessName -Match "update"

 NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName
 ------    -----      -----     ------      --  -- -----------
      0     0.00      10.32     380.46     733   1 SoftwareUpdateN

Method 1: Select-Object

Select-Object reads properties from objects. We can pipe it the object we’ve found:

Get-Process | Where-Object ProcessName -Match "update" | Select-Object cpu


In this case, Where-Object returns one object and Select-Object reads the property off of it, but this still works if we match multiple processes. Then, Where-Object returns an array that gets unpacked by PowerShell’s pipeline and sent to Select-Object one at a time.

Method 2: Subexpression

If we capture the output of our match command into a subexpression, we can access the object’s properties using dotted notation like we would with any variable:

$(Get-Process | Where-Object ProcessName -Match "update").cpu

This also works if our match returned multiple objects. The subexpression will contain an array and PowerShell will automatically get the cpu property from each object in that array.

Method 3: Loop

Like any output, we can pipe our match into a ForEach-Object loop and use the $_ variable to access properties of each item the loop sees:

Get-Process | Where-Object ProcessName -Match "update" | ForEach-Object {$_.cpu}

The loop will of course work on multiple objects.

Happy automating!


Need more than just this article? I’m available to consult.

You might also want to check out these related articles: