Creating Distribution Groups through powershell

I have a script that creates a distribution group.
Before I create the group, I create a list of valid users - i.e. those that don't throw an exception that they were not found in the directory (Cannot find an object with identity: 'xxxxxx' under: 'DC=xxxxx,DC=xxxxxx,DC=com'.)

So I'm populating a list of valid members and a list of invalid members, to notify the customer of the ones that failed.

This has been working great on small lists, but now that I've tested one with 19k items, (only about 12k are valid) it throws an OutOfMemoryException:
Exception of type 'System.OutOfMemoryException' was thrown.
    + CategoryInfo          : OperationStopped: (:) [], OutOfMemoryException
    + FullyQualifiedErrorId : System.OutOfMemoryException

Then, shortly after, my 'user not found exception' seems to change to:
A call to SSPI failed, see inner exception.

What's going on and how I can solve this?

Code snippet:

import-csv $file | Select-Object -ExpandProperty $header | ForEach-Object { $_.trim() } |  ForEach-Object {
            try {
                $members += @(Get-ADUser $_ -ErrorAction SilentlyContinue)
            } catch {
                $nonMembers += $_.exception.message

Open in new window

LVL 67
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.

Gary DewrellSenior Network AdministratorCommented:

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
MASEE Solution Guide - Technical Dept HeadCommented:
sirbountyAuthor Commented:
Ah, wasn't aware of that gdewrell - I'll give it a try on my next run... thanks!
sirbountyAuthor Commented:
thanks MAS - I'll check on those links as well.
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.