Easier way to show a Property

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Jeff Taylor Jeff Taylor 7 months, 2 weeks ago.

  • Author
    Posts
  • #59569
    Profile photo of Jeff Taylor
    Jeff Taylor
    Participant

    This works fine (and it helps me follow the pipeline) but is there a better way to show the Property "Count"?

    Get-ADGroup MyGroup -Server myCompany.com -Properties * | select members -ExpandProperty members | measure | select count
    
    
    Count
    -----
       15

    thank you

  • #59574
    Profile photo of Don Jones
    Don Jones
    Keymaster

    "Better" is in the eye of the beholder. What's wrong with your way? It works.

    • #59586
      Profile photo of Jeff Taylor
      Jeff Taylor
      Participant

      Well, I'm not going to argue with Don Jones, so I'm good, thanks!

  • #59605
    Profile photo of Max Kozlov
    Max Kozlov
    Participant

    if you doesn't glued to pipeline, you can see some tricks here 😉

    # original
    Get-ADGroup MyGroup -Server myCompany.com -Properties * | select members -ExpandProperty members | measure | select count
    # get count as int number, but not object property
    Get-ADGroup MyGroup -Server myCompany.com -Properties * | select members -ExpandProperty members | measure | select -ExpandProperty count
    # but better is
    (Get-ADGroup MyGroup -Server myCompany.com -Properties * | select members -ExpandProperty members | measure).Count
    # save ssome cpu cycles because members already array and have count property
    (Get-ADGroup MyGroup -Server myCompany.com -Properties * | select members -ExpandProperty members).Count
    # save some cpu and network when you don't need all properties but members only and selection redundancy
    (Get-ADGroup MyGroup -Server myCompany.com -Properties members | select -ExpandProperty members).Count
    # and finally 
    (Get-ADGroup MyGroup -Server myCompany.com -Properties members).Members.Count
    # but prepare to $null if you use PSv2.0 because $null.Count == 0 only on PSv3+
    # this can be solved with
    @((Get-ADGroup MyGroup -Server myCompany.com -Properties members).Members).Count
    # but this is harder to read
    
    • #59736
      Profile photo of Jeff Taylor
      Jeff Taylor
      Participant

      ah I was trying to see how I could leverage count as a method but when I saw it as a Property, didn't think I could do something like your example thus:

      (Get-ADGroup MyGroup -Server myCompany.com -Properties * | select members -ExpandProperty members | measure).Count

You must be logged in to reply to this topic.