Author Posts

May 19, 2015 at 7:12 am

Hi,
Could some one tell me how I can expand properties for several at a time in "| Select-Object" cmdlet.

May 19, 2015 at 7:32 am

You can't. That wouldn't actually make sense, as the point of the parameter is to return a single property into the pipeline. Were you to expand multiple properties, there'd be no way to separate the data in the pipeline.

May 19, 2015 at 7:53 am

What are you trying to do? Can you provide a scenario?

May 19, 2015 at 8:34 am

Actually I am writing a production script where I must even log each end every thing. For this I am adding my own results and any other command's results to a PSObject and finally I want to export the content of this PSObject the log file. If I just (for ex: $Output | Export-CSV log.csv) then it will just export the view of $output but it won't expand the properties and then export. The below is the sample which is very ideal to my script.

$temp = Get-Service dns*
$temp1 = Get-Process | select -First 5
If($temp.Status -match 'Stop')
{
   $Ouput | Add-Member -MemberType NoteProperty -Name DNSService -Value 'Fail'
}
Else
{
      $Ouput | Add-Member -MemberType NoteProperty -Name DNSService -Value 'Pass'
}
$Ouput | Add-Member -MemberType NoteProperty -Name 'ActualDNSServiceOutput' -Value $temp
$Ouput | Add-Member -MemberType NoteProperty -Name Process -Value $temp1

[attachment file="Capture.PNG"]

May 19, 2015 at 9:48 am

Keep in mind you've never written $output to the pipeline of your script.

Write-Output $output

If you do that, then you should be able to pipe the script output to Export-CSV and have it work fine. This isn't "expanding properties." -ExpandProperty is a completely different thing.

$temp can contain more than one object, potentially; if it does, your if() construct won't work as expected.

Is $output just a blank PSObject to begin with? I don't see where you create it.

But your screenshot is something else. That shows your object having a Service property, which is a collection of Process objects, which in turn have their own properties. That's a hierarchy; there's no way for a CSV file to represent that. A CSV is a flat file – it doesn't support a hierarchy of information, which is what you've created. There's no way to make a CSV file work with hierarchical data – that isn't what it's for.