there was a question posted along time ago about getting a count of users in an OU. the answer to the questions was great (thank you Eric Nord) and I was able to use it to figure out most of what I need but I was hoping someone could help me figure out the final piece which is to total up the number of users.
here is the script to get the count. I added the sort count to the end of the script
$root_ou = "ou=Users,ou=cmsg,dc=contoso,dc=com"
$User = get-aduser -filter * -SearchBase $root_ou -SearchScope Subtree | Select @{Name="OU";Expression={$_.distinguishedName -match "cn=.*?,OU=(?<OU>.*)" | Out-Null;$Matches.OU}}
$User | Group -Property OU | Select Name,Count | sort count
here is the output
Name Count
Office 1,ou=Users,ou=cmsg,dc=contoso,dc=com 1230
Office 2,ou=Users,ou=cmsg,dc=contoso,dc=com 390
Office 3,ou=Users,ou=cmsg,dc=contoso,dc=com 90
Office 4,ou=Users,ou=cmsg,dc=contoso,dc=com 10
what I need to figure out is how to then add a grand total count at the bottom like this
Name Count
Office 1,ou=Users,ou=cmsg,dc=contoso,dc=com 1230
Office 2,ou=Users,ou=cmsg,dc=contoso,dc=com 390
Office 3,ou=Users,ou=cmsg,dc=contoso,dc=com 90
Office 4,ou=Users,ou=cmsg,dc=contoso,dc=com 10
Grand Total 1720
thank you all.
CodePudding user response:
Add the following statement to construct and output an additional object that contains the overall user count:
[pscustomobject] @{ Name = 'Grand Total'; Count = $User.Count }