runcmd
runcmd

Reputation: 602

Powershell Select-Object Formatting

$output = $data | Where-Object {$_.Name -eq "$serverName"} | Select-Object -Property Description1,Version | Where-Object {$_.Description1 -eq "Power controller Firmware"} | Select-Object -Property Version  

Write-Host $output

Gives me the following output:

    @{Version=3.4}

So $data is an array and I select what I want form it and assign it to a variable to eventually be inputted into a excel file but no matter what I seem to try I cant just select "3.4" Instead it selects like the above (@{Version=3.4}). Doesn't anybody know how to just select the "3.4" within my command?

Upvotes: 2

Views: 7595

Answers (3)

Ann
Ann

Reputation: 1

Please try the following code:

$data | Where-Object {$_.Name -eq "$serverName"} | Select-Object -Property 
Description1, Version | Where-Object {$_.Description1 -eq "Power controller
Firmware"} | write-Host $_.Version  

Upvotes: 0

Chris
Chris

Reputation: 1009

Just replace last line with:

foreach( $out in $output )
{
Write-Host $out.Version
}

In fact your $output variable contains an array so you need to go through it with a foreach loop. Then you can Write-Host or do anything with the Version property.

As stated by @okaram, if you want to make the same kind of looping but after a pipe you can do it this way:

$output | ForEach-Object {Write-Host $_.Version}

or

$output | %{Write-Host $_.Version}

Upvotes: 1

James
James

Reputation: 11931

Your last expression of

Select-Object -Property Version

Keeps the entire object in the pipeline, but filters down the properties to only Version. However, the -ExpandProperty will put the property value itself in the pipeline.

Select-Object -ExpandProperty Version

That should return the "3.4" result you expect.

Upvotes: 0

Related Questions