skipping powershell using default property/Method having same header?

Welcome Forums General PowerShell Q&A skipping powershell using default property/Method having same header?

This topic contains 9 replies, has 4 voices, and was last updated by

 
Participant
2 weeks ago.

  • Author
    Posts
  • #127665

    Participant
    Points: 38
    Rank: Member

    I have a data with Column header as count.
    When I use $variable.count I get the total count of the list/array instead of the count values.

    How do I skip/bypass powershell considering Count as the method.?

    I am not posting the data since it is a simple question.

    This is something I did in the past way ago... but not able to recollect it now 🙁

    Note that I do not want to use

    select @{ expression={$_.Count}; label='Something else' }.....

    I remember there is better way to list count data itself without changing the header name.

  • #127671

    Participant
    Points: 884
    Helping Hand
    Rank: Major Contributor

    Use Select-Object.

    $variable | Select-Object count
    
    #or
    
    $variable | Select-Object -ExpandProperty count
    

    Its always better to not to have column header as same as property name. Count is a property and not a method. Since you $Variable is a collection , it has a property called count.

    • #127672

      Participant
      Points: 38
      Rank: Member

      @KvPrasoon,
      I understand. But there is a lot of situation where you have to deal with it.
      For example, I am pulling an inventory of Devops Udeploy using RESTAPI's.
      And API result contains header named Count, Length, Rank...
      OR
      Importing CSV which has header Count and manipulating it.

      And in response to your reply, I need to use this count inside ForEach($item in $item.count){}
      Note that this count in $item.count is the header.

      Now, I may not use select object inside for each...

      With all that said. I know there was a easier method 🙂 something like $variable.'Count' or $variable.[count].... . which I am not able to recollect.

  • #127677

    Participant
    Points: 324
    Helping Hand
    Rank: Contributor

    I think you are looking for Measure-Object:

    PS C:\Users\Rob> $processes = Get-Process | Select Handles -First 10
    
    
    PS C:\Users\Rob> $processes
    
    Handles
    -------
        589
       1097
        542
        540
        241
        225
        594
        483
        692
       1214
    
    
    
    PS C:\Users\Rob> $processes | Measure-Object -Property Handles -Sum | Select -ExpandProperty Sum
    
    6217
    
    
    • #127678

      Participant
      Points: 38
      Rank: Member

      Thanks Rob for the response. But I am not looking for this. Ok Let me clarify.
      Lets say I have below data in $temp
      Count Product
      —–      ——-
      2        Apple
      3        Orange
      4        Mango

      Now $temp.count will give me output 3. $temp | Select Count will give me the values under header count

      But I need this to be used inside foreach($item in $temp.count){}
      It wont work since $temp.Count results to 3.. ...... Hope this is clear...

  • #127680

    Participant
    Points: 884
    Helping Hand
    Rank: Major Contributor

    You can still do this with foreach

     foreach($item in $temp | Select-Object -ExpandProperty count)
    
    • #127681

      Participant
      Points: 38
      Rank: Member

      That works .... ... Times smaller things break your head 🙂 well I was since last 2 hours...
      Thank you very much..

  • #127684

    Participant
    Points: 105
    Helping Hand
    Rank: Participant

    You can get to it through its enumerator... $temp.GetEnumerator().Count

    • #127692

      Participant
      Points: 38
      Rank: Member

      Perfect... this is what i was trying to recollect....

  • #127686

    Participant
    Points: 884
    Helping Hand
    Rank: Major Contributor

    $temp.GetEnumerator().Count 👍

You must be logged in to reply to this topic.