Getting AD object count from all partitions

Welcome Forums General PowerShell Q&A Getting AD object count from all partitions

This topic contains 1 reply, has 2 voices, and was last updated by

4 years, 6 months ago.

  • Author
  • #20416

    Topics: 23
    Replies: 140
    Points: 30
    Rank: Member

    I have a requirement to find the total number of objects in Active Directory broken down by naming context. Currently, I have something like this:

    $NamingContexts = [Get-AdRootDse].namingContexts
    $AdObjects = [system.Collections.ArrayList]@[]
    			foreach [$Nc in $NamingContexts] {
    				$Objects = Get-AdObject -Filter * -SearchBase $Nc
    				foreach [$Object in $Objects] {
    					$Object | Add-Member -MemberType 'NoteProperty' -Name 'NamingContext' -Value $Nc -Force
    					$AdObjects.Add[$Object] | Out-Null
    			$AdObjects | Group-Object NamingContext

    I'm not proud of it because it's horribly inefficient but I can't think of a better way. Anyone have any tips?

  • #20431

    Topics: 9
    Replies: 2322
    Points: 0
    Rank: Member

    If all you care about is the count, you don't need to store or group the actual objects. Something like this should be fine:

    $NamingContexts = (Get-AdRootDse).namingContexts
    $report = 
    foreach ($Nc in $NamingContexts) {
        [pscustomobject] @{
            Context     = $Nc
            ObjectCount = (Get-AdObject -Filter * -Properties distinguishedName -SearchBase $Nc).Count
    $report | Format-Table -AutoSize

    You may also find that this is one of the situations where it's preferable to use the ADSI classes instead of the AD module cmdlets, which tend to be quite a bit slower. Setting up an [adsisearcher] object and calling FindAll().Count would likely be a performance improvement.

The topic ‘Getting AD object count from all partitions’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort