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?
 
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
 
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
 
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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 Specialist/DeveloperCommented:
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
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.

All Courses

From novice to tech pro — start learning today.