Powershell script looking for user count


I am running the following powershell script and it is giving an error "Invalid enumeration context", in get-aduser. I have close to 800,000 user objects in the domain.

$AllUsers = get-aduser -filter *
$TotalUserCount = $AllUsers.count

Could you please suggest how to get this to work. This script worked in other smaller domains.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
the default limit is 1000 users you can use the
get-aduser -resultsetsize $null to return all

$AllUsers = get-aduser -filter * -resultsetsize $null
$TotalUserCount = $AllUsers.count

Open in new window

Quest Tools get-qaduser -sizelimit 0 is the equivalent
Parity123Author Commented:
I tested this. It looks like it is running for a while and then terminates with error cannot contact server, unless the connection times out after a while.

Are there any other options
Parity123Author Commented:
I still get the same error - invalid enumeration context
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

What David Johnson post earlier is very similar to the examples in the offing like but they use brackets

David Johnson, CD, MVPOwnerCommented:
that means that it is timing out .. the reason for the limit was to not overload your DC. use a filter i.e. run it 36 times  
get-aduser -Filter 'samaccountname -like "a*" '

Open in new window

$usercount = 0
$lettercount = $firstletter.Length

for($i=0; $i -le $lettercount; $i++){
$filter = $FirstLetter[$i] + "*"
$users = get-aduser -Filter 'samaccountname -like $filter'
    if ($users.Count -ne $null){
        $usercount += $users.Count

Open in new window

Parity123Author Commented:
Thanks all for your responses. The following worked for me.

$allusers = get-aduser -filter * | measure | select count
$allusercount = $allusers.count

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Parity123Author Commented:
I tried a few things
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.