bearaman
bearaman

Reputation: 1091

How can I select a single property from PowerShell Expandproperty

I have a simple PowerShell expression that returns some object properties. It also returns an object called Computer which has a property called ComputerName which I'm trying to access. The ExpandProperty switch returns all the properties. How do I filter it to return the properties I need?

Here's the expression:

foreach ($computer in $jobinstancestatus.Computers) {
    Select-Object -InputObject $computer -property Status,ResultsPath -ExpandProperty Computer
}

I tried this but it gave an error when I tried to run it.

foreach ($computer in $jobinstancestatus.Computers) {
    Select-Object -InputObject $computer -property Status,ResultsPath (-ExpandProperty Computer).ComputerName
}

Upvotes: 2

Views: 4148

Answers (1)

Mike Shepard
Mike Shepard

Reputation: 18156

Try this (inside the loop):

Select-Object -InputObject $computer -property Status,ResultsPath,@{N='Computer_ComputerName';E={$_.Computer.ComputerName}}

It's using a "calculated property" to get the value you need.

Upvotes: 4

Related Questions