Counting the Number of AD User Accounts in PowerShell

powershell cim 1 - Counting the Number of AD User Accounts in PowerShell

powershell cim 1 600x337 - Counting the Number of AD User Accounts in PowerShell

Here are some PowerShell examples that we can use to count the numbers of user accounts in Active Directory.

Total number of user accounts in AD

PS> (Get-ADUser -filter *).count

Total number of user accounts in an OU

PS> (Get-ADUser -filter * -searchbase "OU=Vancouver, OU=MyCompany, DC=Domain, DC=Local").count

Replace the SearchBase with your own OU path.

Total number of enabled/disabled accounts in AD

PS> (Get-AdUser -filter * |Where {$_.enabled -eq "True"}).count
PS> (Get-ADUser -filter * |Where {$_.enabled -ne "False"}).count

Total number of user accounts in a Group

PS> (Get-ADGroupMember -Identity "Group Name").count

PowerShell count AD User Accounts - Counting the Number of AD User Accounts in PowerShell

I ran these on a Windows 10 machine. If you get an error message saying the unrecognized command, follow this instruction to get PowerShell Active Directory Module installed.

7 thoughts on “Counting the Number of AD User Accounts in PowerShell

  1. hi,
    is that possible to count the aduser every 1000 objects and parse to csv file every completed 1000 objects and continue again after parsing to csv file?
    please advise on how to achieve and how to construct in powershell
    Thanks

  2. This code for “Total number of enabled/disabled accounts in AD” has too many errors…
    Yes it works but it is not clear on first look why.
    And you can find it via google… So here is the correct version:

    (Get-AdUser -filter ‘Enabled -eq $true’).count # This gets the number of enabled accounts
    The difference here is filtering on the AD site with a filter instead of using -Filter * and more importantly not comparing a boolean value (Enabled) to a string since casting string to bool nearly always equals $true
    Same with the disabled accounts:
    (Get-AdUser -filter ‘Enabled -eq $false’).count
    Same stuff here. Use a filter on the AD site of things. Compare with actual boolean values. Don’t switch around the comparison ‘-ne’ vs ‘-eq’ in the first example to make the commands understandable.

Leave a Reply to Marlon Cancel reply

Your email address will not be published. Required fields are marked *