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 } } }
Check
for ($i = 1; $i -le 17; $i++) { if ($i -ge 15) { $count = Get-QueryResult -Id $i } else { $count = (Get-QueryResult -Id $i | Measure-Object).Count } [PSCustomObject]@{ Query = $i Count = $count } }
Measure
for ($i = 1; $i -le 17; $i++)
{
New-Variable -Name “query$i” -Value $(’{0:N2}’ -f (Measure-Command -Expression { Get-QueryResult -ID $i }).TotalSeconds)
}
[PSObject]@{
‘Get-ADUser -Filter objectClass and objectCategory’ = $query1
‘Get-ADUser -LDAPFilter objectclass objectcategory’ = $query2
‘Get-ADObject -Filter objectClass and objectCategory’ = $query3
‘Get-ADObject -LDAPFilter objectclass objectcategory’ = $query4
‘Get-ADObject -LDAPFilter sAMAccountType=805306368’ = $query5
‘Quest’ = $query6
‘[adsisearcher]’ = $query7
‘Find-LdapObject objectClass and objectCategory PropertiesToLoad’ = $query8
‘Find-LdapObject sAMAccountType=805306368 PropertiesToLoad’ = $query9
‘Find-LdapObject objectClass and objectCategory’ = $query10
‘Find-LdapObject sAMAccountType=805306368’ = $query11
‘dsquery user -o samid’ = $query12
‘dsquery objectClass and objectCategory’ = $query13
‘dsquery sAMAccountType=805306368’ = $query14
‘adfind objectClass and objectCategory’ = $query15
‘adfind sAMAccountType=805306368’ = $query16
‘adfind -sc adobjcnt:user’ = $query17
}.GetEnumerator() | Sort-Object -Property Value | Select-Object -Property @{
Name = ‘Query’
Expression = {$.Name}
}, @{
Name = ‘TotalSeconds’
Expression = {[double]$.Value}
} | Sort-Object -Property TotalSeconds | Format-Table -AutoSize
`First, I check that all these commands return the same value:
http://bit.ly/2bNwpXl
Result:
http://bit.ly/2bGT9pK
- Conclusion
- In this scenario, the fastest was :
AdFind.exe -b 'DC=domain,DC=com' -f 'sAMAccountType=805306368' -c Links :
Download AdFind (adfind.exe)
http://www.joeware.net/freetools/tools/adfind/
Download System.DirectoryServices.Protocols module (S.DS.P.psm1)
https://gallery.technet.microsoft.com/scriptcenter/Using-SystemDirectoryServic-0adf7ef5
Download QAD cmdlets (Get-QADUser)
http://software.dell.com/products/activeroles-server/powershell.aspx
All these tools in one file :
http://bit.ly/2bnCRVk
Note : If you have a faster solution, feel free to comment below so I can update my article.
**
Real-world example
**:
Couting the total numbers of users in Active Directory can be useful in some cases.
You could need this information to generate statistics or reports, or maybe you just want to monitor the number of accounts created / removed on regular basis.
Related Articles
PowerShell Escape Room
PowerShell Escape Room by Michiel Hamers
by Michiel Hamers
https://about.me/michielhamers/
Why on earth you want to create an Escape Room with PowerShell as backend?
I’ve always been a fan of escape rooms, so I decided to create my own for my kids. I wanted to make it something that would be challenging and fun for them, but also educational. I decided to use PowerShell as the backend for the escape room, as I’m a PowerShell developer and I thought it would be a great way to learn more about the language.
The first step was to design the rooms. I wanted to make sure that there were a variety of puzzles and challenges that my kids would have to solve. I also wanted to make sure that the rooms were visually appealing and engaging. Once I had the rooms designed, I started building them.
I used a variety of materials to build the rooms, including wood, cardboard, and fabric. I also used a few electronic components, such as a USB extension cable with a switch and a 3-button keyboard. The USB extension cable with a switch was used to create a physical button that my kids could press to solve one of the puzzles. The 3-button keyboard was used to enter the code that my kids had to find to solve another puzzle.
I also used a few websites to create rebus puzzles that my kids had to solve. I printed out the rebus puzzles and placed them around the rooms. Once my kids had solved all of the puzzles, they were able to enter the code on a single screen to escape the room.
In this blog post, we’ll delve into the process of creating an engaging PowerShell escape room for the global PowerShell community. We’ll emphasize the significance of storytelling and provide a detailed breakdown of the PowerShell structure used for the escape room.
Microsoft Graph PowerShell Module: Getting Started Guide
Microsoft Graph PowerShell Module: Getting Started Guide
by Jeff Brown
Microsoft is retiring the Azure AD Graph API sometime after June 30, 2023 (announcement). This retirement includes the Azure AD PowerShell module. In its place, Microsoft has released the Microsoft Graph PowerShell module. The Microsoft Graph PowerShell module is the next-generation way of managing Microsoft cloud services using PowerShell. If you have used MSOnline or Azure AD PowerShell in the past, you’ll need to read on to learn about this new module.
ICYMI: PowerShell Week of 08-October-2021
Topics include VMWare, Windows 11, Web Reports and more…
Special thanks to Robin Dadswell, Prasoon Karunan V, Kiran Patnayakuni and Kevin Laux
How to gather your vCenter inventory data with this VMware PowerShell script
by Scott Matteson on 7th October
Inventory reports are a common request when administering a VMware vCenter environment. Learn how this VMware PowerShell script can make such requests quick and easy
Building a Web Report in PowerShell, use the -Force Luke
by Chris Noring on 8th October
