Powershell script looking for user count

Hello,

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.

Thanks
Parity123Asked:
Who is Participating?
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
0
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
0
Parity123Author Commented:
I still get the same error - invalid enumeration context
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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

http://support.risualblogs.com/blog/2012/07/24/view-number-of-user-accounts-in-ad-via-ad-powershell/
0
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

$firstletter="abcdefghijklmnopqrstuvwxyz0123456789"
$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
    }
}
$usercount

Open in new window

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

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

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
0
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
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.