Why do I get different count values using Get-ADObject and Get-ADUser?

Welcome Forums General PowerShell Q&A Why do I get different count values using Get-ADObject and Get-ADUser?

This topic contains 5 replies, has 3 voices, and was last updated by

 
Participant
4 years, 2 months ago.

  • Author
    Posts
  • #18964

    Participant
    Points: 1
    Rank: Member

    I am having a WTF moment here.

    Can someone please explain to me why these two produce different count results.

    Thank you very much.

    Using Get-ADObject

    The following code for my environment produces a value of 2603:

    $GetADObject = Get-ADObject -Filter 'ObjectCategory -eq "User"'

    $ADObjectCount = $GetADObject.Count

    $ADObjectCount

    Using Get-ADUser

    The following code for my environment produces a value of 2562

    $GetADUser = Get-ADUser -Filter *

    $GetADUser.Count

  • #18965

    Participant
    Points: 3
    Rank: Member

    The different number is because the user category contains also computer accounts, not only user accounts. In order to obtain the result you want you have to use ObjectCategory -eq "Person".

  • #18966

    Participant
    Points: 1
    Rank: Member

    AAHHHHHHH geez, I completely forgot about the "Person" identifier.

    Sweet!

    Thanks man

  • #18967

    Participant
    Points: 1
    Rank: Member

    Nope, it's still off by the same values. Anything else it could be?

    Should I just go with Get-ADUser instead of Get-ADObject?

    Thanks again

  • #18971

    Member
    Points: 0
    Rank: Member

    To retrieve only user accounts, you need to filter on both objectCategory = 'Person' and objectClass = 'User'. There are probably other combinations which get a similar result, but this is how I learned it, back in the Windows 2000 Active Directory days:

    $users = Get-ADObject -Filter 'objectCategory -eq "person" -and objectClass -eq "user"'
    
  • #18988

    Participant
    Points: 1
    Rank: Member

    Sounds good. Thanks again, Dave

The topic ‘Why do I get different count values using Get-ADObject and Get-ADUser?’ is closed to new replies.