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 Profile photo of I Am Sir Ask Alot I Am Sir Ask Alot 2 years, 2 months ago.

  • Author
    Posts
  • #18964
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    I am having a WTF moment here.

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

    Thank you very much.

    [u][b]Using Get-ADObject
    [/b][/u]

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

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

    $ADObjectCount = $GetADObject.Count

    $ADObjectCount

    [u][b]Using Get-ADUser[/b][/u]

    The following code for my environment produces a value of 2562

    $GetADUser = Get-ADUser -Filter *

    $GetADUser.Count

  • #18965
    Profile photo of adi dumitras
    adi dumitras
    Participant

    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
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

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

    Sweet!

    Thanks man

  • #18967
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    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
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    Sounds good. Thanks again, Dave

You must be logged in to reply to this topic.