Mar 21 2017

Count Results in PowerShell

Published by at 9:45 pm under Windows





 

I was astonished to see PowerShell would display nothing when it should count 0 or 1.
 
Here’s a screenshot retrieving the number of AD accounts.
 
Instead use the Measure cmdLet and select the count value:

C:\>(Get-ADUser -filter {SamAccountName -like "dron*"} |
     select SamAccountName | measure).count
1

 

 
Another useful command:
The “tee” command lets you store the result set into a variable and its number of elements into another:
 

C:\>$accountnb = (Get-ADUser -filter {SamAccountName -like "dr*"} |
                 select SamAccountName |
                 tee -Variable accounts | measure).count
C:\>$accountnb
3
C:\>$accounts

SamAccountName                                                                                                     
--------------                                                                                                     
draguene                                                                                                           
drondy                                                                                                             
droze                                                                                                              

No responses yet

Trackback URI | Comments RSS

Leave a Reply