ChatGPT解决这个技术问题 Extra ChatGPT

How to count objects in PowerShell?

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, running get-alias returns me a number of System.Management.Automation.AliasInfo objects:

PS Z:\> get-alias

CommandType     Name                                             Definition
-----------     ----                                             ----------
Alias           %                                                ForEach-Object
Alias           ?                                                Where-Object
Alias           ac                                               Add-Content
Alias           asnp                                             Add-PSSnapIn
Alias           cat                                              Get-Content
Alias           cd                                               Set-Location
Alias           chdir                                            Set-Location
...

Now, how do I get the count of these objects?

(Get-Alias).Count

j
jumbo

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)


I used $Counter = $(get-alias | measure).Count as it always returns the value 1 in case there's only one occurrence, just as you said.
Another option: get-alias | measure | % { $_.Count }
fyi: measure is short for Measure-Object; and returns a lot of other stuff -- like Average, Sum, Maximum, etc...
Why not just (gal).count? Requires PS v3 or higher IIRC. EDIT: this has already been answered.
@Shameer While we're at it, one can use Get-Alias | Measure-Object | Select-Object -ExpandProperty Count to get rid of % and {}. What is easier to write and read is for another discussion.
s
svick

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:

You can put an arbitrarily complex expression in place of Get-Alias, for example: @(Get-Process | ? { $_.ProcessName -eq "svchost" }).Count 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.


I know this is old, but for others who go looking for this… v3+ doesn't seem to work without the @ for count < 2 if Set-StrictMode -Version Latest is used. I just ran into this today (on 5.1) — something worked interactively that didn't work in a function. I tracked it down to the function have Set-StrictMode in it; when I set strict mode interactively, it didn't work, either.
Good catch, @vr8ce! I am a strong advocate of Set-StrictMode, too, so I should have noticed that.
To expand on @vr8ce's advice, I couldn't use @($myObject) to get a count on an existing object. So I use an if ($myObject.PSobject.Properties.Name -contains "count") (from stackoverflow.com/questions/26997511/…) before trying to test the count property. I hate that PoSH makes me do this but Strict Mode is worth it.
m
makerofthings7

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

(get-alias).count

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.
@Damien OK. I made the edit . In SO people generally come from the first answer to the last. They wont suddenly jump into the third answer and get confused. So that I made it as a continuation of first two answers. That answer makes more sense then this edited one.
This is already covered in this answer. Your basically just re-iterating whats already been said two years after it's been said.
@Liam That answer is for powershell v2. My answer is for powershell v3. As you said this answer is covered/told literally in the update(2012.01.30 Update) in that answer. I just gave coding part.
b
bchris999

@($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.


Chris, I think to get what you wanted out that you'd need to do this: @(Get-VmQuestion -VM vm1).count You can't force the output to be an array... once the output is already made. Which is what you are doing.
z
zaerymoghaddam

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

You've shared good info about pulling information from Exchange using PowerShell, and hidden in there is a count, making this a valid answer but not to this question.
If you look attentively enough, you'll see that all 4 answers give the @(...).Count syntax.
u
ulidtko
Get-Alias|ForEach-Object {$myCount++};$myCount

158