Link to home
Start Free TrialLog in
Avatar of Parity123
Parity123Flag for United States of America

asked on

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
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

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
Avatar of Parity123

ASKER

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
I still get the same error - invalid enumeration context
Avatar of compdigit44
compdigit44

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/
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

ASKER CERTIFIED SOLUTION
Avatar of Parity123
Parity123
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I tried a few things