The fastest Powershell #1 : Count all users in Active Directory domain
Updated : October 01, 2015
**
Question
**: What is the fastest solution to count all the users in Active Directory domain?
** Answer **: To answer this question, I will compare 17 different commands in a domain with 75 000 users.
`[System.GC]::WaitForPendingFinalizers() [System.GC]::Collect() Set-Location -Path ‘C:\demo’ Add-Type -AssemblyName System.DirectoryServices.Protocols Import-Module -Name .\S.DS.P.psd1 Add-PSSnapin -Name ‘Quest.ActiveRoles.ADManagement’ $searcher = [adsisearcher]’(&(objectclass=user)(objectcategory=person))' $searcher.SearchRoot = ‘LDAP://DC=domain,DC=com’ $searcher.PageSize = 1000 $searcher.PropertiesToLoad.AddRange((‘samaccountname’)) function Get-QueryResult { [CmdletBinding()] Param ( [Parameter(Mandatory=$true)] [int]$Id ) switch ($id) { 1 { ( Get-ADUser -Filter ‘objectClass -eq “user” -and objectCategory -eq “person”’ -SearchBase ‘DC=domain,DC=com’ -Properties SamAccountName).SamAccountName } 2 { ( Get-ADUser -LDAPFilter ‘(&(objectclass=user)(objectcategory=person))’ -SearchBase ‘DC=domain,DC=com’ -Properties SamAccountName).SamAccountName } 3 { ( Get-ADObject -Filter ‘objectCategory -eq “person” -and objectClass -eq “user”’ -SearchBase ‘DC=domain,DC=com’ -Properties SamAccountName).SamAccountName } 4 { ( Get-ADObject -LDAPFilter ‘(&(objectclass=user)(objectcategory=person))’ -SearchBase ‘DC=domain,DC=com’ -Properties SamAccountName).SamAccountName } 5 { ( Get-ADObject -LDAPFilter ‘sAMAccountType=805306368’ -SearchBase ‘DC=domain,DC=com’ -Properties SamAccountName).SamAccountName } 6 { ( Get-QADUser -SearchRoot ‘DC=domain,DC=com’ -DontUseDefaultIncludedProperties -IncludedProperties SamAccountName -SizeLimit 0).SamAccountName } 7 { ( $searcher.FindAll() ) } 8 { (Find-LdapObject -SearchFilter:’(&(objectclass=user)(objectcategory=person))’ -SearchBase:‘DC=domain,DC=com’ -LdapServer:’’ -PageSize 1000 -PropertiesToLoad:@(‘sAMAccountName’)) } 9 { (Find-LdapObject -SearchFilter:‘sAMAccountType=805306368’ -SearchBase:‘DC=domain,DC=com’ -LdapServer:’’ -PageSize 1000 -PropertiesToLoad:@(‘sAMAccountName’)) } 10 { (Find-LdapObject -SearchFilter:’(&(objectclass=user)(objectcategory=person))’ -SearchBase:‘DC=domain,DC=com’ -LdapServer:’’ -PageSize 1000) } 11 { (Find-LdapObject -SearchFilter:‘sAMAccountType=805306368’ -SearchBase:‘DC=domain,DC=com’ -LdapServer:’’ -PageSize 1000) } 12 { (dsquery user -o samid ‘DC=domain,DC=com’ -limit 0) } 13 { (dsquery * -filter ‘(&(objectclass=user)(objectcategory=person))’ -attr samAccountName -attrsonly -limit 0) } 14 { (dsquery * -filter ‘sAMAccountType=805306368’ -attr samAccountName -attrsonly -limit 0) } 15 { ([regex]::match((.\AdFind.exe -b ‘DC=domain,DC=com’ -f ‘(&(objectclass=user)(objectcategory=person))’ -c),’\d{5}’).value) 2> $null } 16 { ([regex]::match((.\AdFind.exe -b ‘DC=domain,DC=com’ -f ‘sAMAccountType=805306368’ -c),’\d{5}’).value) 2> $null } 17 { ([regex]::match((.\AdFind.exe -b ‘DC=domain,DC=com’ -sc adobjcnt:user -c),’\d{5}’).value) 2> $null } } }
