Powershell script to delete old AD Computers accounts failing

Hello all and thanks in advance for your time and expertise.  
In a nutshell i have to cleanup our Active Directory as there are computer accounts going back to 2009.  I ran a powershell script for the lastlogondate for these computers and had it create a text file.
I've tried running the following two powershell scripts to delete these accounts pointing at my text file but no success either time.
- First Powershell script:  Get-Content C:\oldcomputers.txt | % { Get-ADComputer -Filter { Name -eq $_ } } | Remove-ADObject -Recursive  (For some reason when I run this script there's a short pause and then I'm taken to the command prompt in Powershell and nothing happens relative to deletion of accounts.
- Second Powershell script:  Get-Content oldcomputers.txt | Get-Computer | Remove-AdObject -recursive   (When I run this script I get the following error message for the computer accounts:  FullyQualifiedErrorId:  cannot find an object with identity:  'computername
I would appreciate your help getting this situation resolved.  Thanks.
pendal1IT ManagerAsked:
Who is Participating?

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

x
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.

Richie KnightSenior Network AnalystCommented:
I suspect it doesn't like the formatting of your oldcomputers.txt file. Can you please post an eaxmple of what the data looks like in it?
1
oBdACommented:
This should do the trick; it's in test mode and will only display what it would delete.
Remove the -WhatIf at the end of the line to run it for real.
The file needs one name per line, either the host name or the SamAccountName (with a $ at the end), no double quotes around the names.
Get-Content C:\oldcomputers.txt | Remove-ADComputer -Confirm:$false -Verbose -WhatIf

Open in new window

1

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
pendal1IT ManagerAuthor Commented:
I agree with you folks.  My text file has the computer names and the last logon date on the same line.  The powershell I ran to abstract the lastlogondate for the computers included this data on the same line.  Not sure there's an easy way to clean up this text file.  When I copy it into excel all of the data (computer name and lastlogondate) are in one cell for each entry.
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Richie KnightSenior Network AnalystCommented:
That's a pain. You should only have the computer names in the oldcomputers.txt file.
1
oBdACommented:
That can be cleared, but you need to post a sample of the file, that is, the first three lines.
Change only the computer names, nothing else. No formatting.
Copy and paste it here inside a [code][/code] block.
1
Shaun VermaakTechnical SpecialistCommented:
Sure, you can do that in PowerShell. If that is not important to you have a look at my ADCleanup tool
https://www.experts-exchange.com/articles/30820/Active-Directory-Cleanup-Tool-ADCleanup.html
1
pendal1IT ManagerAuthor Commented:
Thank you fellas.  The problem was my text file.  Your quick responses are much appreciated.
0
Naveen SharmaCommented:
Powershell to Find Inactive AD Users and Computers Accounts:
http://expert-advice.org/active-directory/powershell-to-find-inactive-ad-users-and-computers-accounts/

You may also get help from this Active Directory Cleanup solution to identify and clean up inactive Active Directory user accounts.
1
pendal1IT ManagerAuthor Commented:
Thank you for your assistance.
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.