Select-Object -expandproperty SomeArray

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of marcoolinho Pappas marcoolinho Pappas 1 year, 6 months ago.

  • Author
    Posts
  • #29655
    Profile photo of marcoolinho Pappas
    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
    Profile photo of Sean Quinlan
    Sean Quinlan
    Participant

    (Get-CompInfo -ComputerName MyPC).DiskInfo

  • #29671
    Profile photo of Rob Simmers
    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
    Profile photo of marcoolinho Pappas
    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.