Select-Object -expandproperty SomeArray

This topic contains 3 replies, has 3 voices, and was last updated by  marcoolinho Pappas 2 years, 1 month ago.

  • Author
    Posts
  • #29655

    marcoolinho Pappas
    Participant

    Hi,

    can anyone please tell me the short version of expanding values in an array (property with squiggly brackets and lots of values in it)?

    For example,
    Get-CompInfo -ComputerName MyPC | Select-Object -expandproperty DiskInfo

    How do I shorten the second part of the command after the pipe?

    I've seen someone do it somewhere in some video demo but I can't remember.

    Thank you very much

  • #29662

    Sean Quinlan
    Participant

    (Get-CompInfo -ComputerName MyPC).DiskInfo

  • #29671

    Rob Simmers
    Participant

    Sean's example is a implicit loop, which requires Powershell v3 or higher. Rather than doing this:

    Get-CompInfo -ComputerName MyPC | foreach{$_.DiskInfo}
    

    the implicit foreach was provided as a shortcut to do the same thing.

  • #29678

    marcoolinho Pappas
    Participant

    Thank you good people.

    @Rob

    Get-CompInfo -ComputerName MyPC | % DiskInfo

    That's what I saw in some video! I remembered now once I saw your "foreach{$_.DiskInfo}".

    thank you

You must be logged in to reply to this topic.