How to count objects in PowerShell?

后端 未结 5 2157
星月不相逢
星月不相逢 2020-12-07 14:17

As I\'m reading in the PowerShell user guide, one of the core PowerShell concepts is that commands accept and return objects instead of text. So for example, runnin

相关标签:
5条回答
  • 2020-12-07 14:51

    @($output).Count does not always produce correct results. I used the ($output | Measure).Count method.

    I found this with VMware Get-VmQuestion cmdlet:

    $output = Get-VmQuestion -VM vm1
    @($output).Count
    

    The answer it gave is one, whereas

    $output
    

    produced no output (the correct answer was 0 as produced with the Measure method).

    This only seemed to be the case with 0 and 1. Anything above 1 was correct with limited testing.

    0 讨论(0)
  • 2020-12-07 14:51

    in my exchange the cmd-let you presented did not work, the answer was null, so I had to make a little correction and worked fine for me:

    @(get-transportservice | get-messagetrackinglog -Resultsize unlimited -Start "MM/DD/AAAA HH:MM" -End "MM/DD/AAAA HH:MM" -recipients "user@domain.com" | where {$_.Event
    ID -eq "DELIVER"}).count
    
    0 讨论(0)
  • 2020-12-07 15:01

    This will get you count:

    get-alias | measure
    

    You can work with the result as with object:

    $m = get-alias | measure
    $m.Count
    

    And if you would like to have aliases in some variable also, you can use Tee-Object:

    $m = get-alias | tee -Variable aliases | measure
    $m.Count
    $aliases
    

    Some more info on Measure-Object cmdlet is on Technet.

    Do not confuse it with Measure-Command cmdlet which is for time measuring. (again on Technet)

    0 讨论(0)
  • 2020-12-07 15:02

    Just use parenthesis and 'count'. This applies to Powershell v3

    (get-alias).count
    
    0 讨论(0)
  • 2020-12-07 15:09

    As short as @jumbo's answer is :-) you can do it even more tersely. This just returns the Count property of the array returned by the antecedent sub-expression:

    @(Get-Alias).Count
    

    A couple points to note:

    1. You can put an arbitrarily complex expression in place of Get-Alias, for example:

      @(Get-Process | ? { $_.ProcessName -eq "svchost" }).Count
      
    2. The initial at-sign (@) is necessary for a robust solution. As long as the answer is two or greater you will get an equivalent answer with or without the @, but when the answer is zero or one you will get no output unless you have the @ sign! (It forces the Count property to exist by forcing the output to be an array.)

    2012.01.30 Update

    The above is true for PowerShell V2. One of the new features of PowerShell V3 is that you do have a Count property even for singletons, so the at-sign becomes unimportant for this scenario.

    0 讨论(0)
提交回复
热议问题