Piping into remove-aduser

Hi Experts,

Why is this script not working:

:\> gc c:\usersremove.txt | % {get-aduser -filter 'Name -like $_ '} | ft SamAccountName | % {remove-aduser $_ -confirm:$false}
LVL 9
dipopoAsked:
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.

dipopoAuthor Commented:
Also tried this but same result, must be missing something.

gc C:\removesamaccount.txt | remove-aduser -confirm:$false

and the above is the samaccountsname in a text file.
0
SubsunCommented:
gc c:\usersremove.txt | % {get-aduser -filter 'Name -like $_ '} | ft SamAccountName | % {remove-aduser $_ -confirm:$false}

Open in new window


Here you have used FT which wont pass the object to pipeline.. use select object instead of FT

For example..
gc c:\usersremove.txt | % {get-aduser -filter 'Name -like $_ '} | Select SamAccountName | % {remove-aduser $_.SamAccountName -confirm:$false}

Open in new window

Or
gc c:\usersremove.txt | % {get-aduser -filter 'Name -like $_ ' | remove-aduser -confirm:$false}

Open in new window


or
gc C:\removesamaccount.txt | %{remove-aduser $_ -confirm:$false}

Open in new window

Ref :http://technet.microsoft.com/en-us/library/ee617206.aspx
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
dipopoAuthor Commented:
Error generated is:

Remove-ADUser : Cannot find an object with identity: 'bracken
                                                      ' under: 'DC=xxxxxxxxx,DC=xxx'.
At line:1 char:46
+ gc C:\removesamaccount.txt | % {remove-aduser <<<<  "$_" -confirm:$false}


but running the command with the samaccountname works!

remove-aduser bracken -confirm:$false

hence what is the difference as the only thing that is different is one is piped and the other is not!!!!
0
The Ultimate Tool Kit for Technolgy Solution Provi

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 for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

SubsunCommented:
Do you have any trailing space in the text file data?
0
dipopoAuthor Commented:
Thank you Subsun this worked wonderfully!

gc c:\usersremove.txt | % {get-aduser -filter 'Name -like $_ ' | remove-aduser -confirm:$false}

I'm baffled though as I tried this before and it did not work....oh well, all is well that ends well.

Thanks again.
0
SubsunCommented:
Nice..  BTB any particular reason for the B grade?

Please see the EE guidelines on grading..
http://support.experts-exchange.com/customer/portal/articles/481419
0
dipopoAuthor Commented:
Opps my bad, anyway I can change it?
0
SubsunCommented:
No problem.. I think only Mod's can change it.. you may use Request Attention option to create a request with Mod's..
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.