Bulk deletion of computer and user accounts in Active Directory not working

Indie101
Indie101 used Ask the Experts™
on
Looking to delete computer accounts and user accounts in Active Directory

Got below script for computer accounts previously

Import-Csv -Path C:\Temp\DisableComputer.csv | ForEach-Object {Remove-ADComputer -Identity $_.Name -WhatIf}

It ran fine above then I changed it to below to run it properly

Import-Csv -Path C:\Temp\DisableComputer.csv | ForEach-Object {Remove-ADComputer -Identity $_.Name}

Problem is it doesnt perform deletion of computer accounts in a batch, have to do it one by one and see below error

Remove-ADComputer : The directory service can perform the requested operation only on a leaf object

Looking for a way to bulk delete and also modification required to bulk delete AD user accounts also
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
MaheshArchitect
Distinguished Expert 2018

Commented:
Try adding force parameter to cmdlet

Import-Csv -Path C:\Temp\DisableComputer.csv | ForEach-Object {Remove-ADComputer -Identity $_.Name -Force}

Open in new window


OR

Import-Csv -Path C:\Temp\DisableComputer.csv | ForEach-Object {Remove-ADComputer -Identity $_.Name -Confirm:$false}

Open in new window

Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Use Remove-ADObject and -Recursive like this:
Import-Csv -Path C:\Temp\DisableComputer.csv | ForEach-Object {Get-ADComputer -Identity $_.Name | Remove-ADObject -Recursive -Confirm:$false}

Open in new window

Author

Commented:
Thanks get below error when I use that

Remove-ADComputer : A parameter cannot be found that matches parameter name 'Force'.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Thanks oBDA the columns are "Name","DNSHostName","LastLogondate","DistinguishedName" will it work with this?
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
If you already have the DN, it's even easier:
Import-Csv -Path C:\Temp\DisableComputer.csv | ForEach-Object {Remove-ADObject $_.DistinguishedName -Recursive -Confirm:$false}

Open in new window

Author

Commented:
Thanks oBdA the first one worked fine, looking for one for the user accounts now if possible
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Since they're all AD objects, the exact same command can be used for user objects, too.
Or with Remove-ADUser, assuming you have a DistinguishedName column there as well:
Import-Csv -Path C:\Temp\DisableUsers.csv | ForEach-Object {Remove-ADUser -Identity $_.DistinguishedName}

Open in new window

Author

Commented:
Thanks oBdA for AD users I only have the samaccountname so I should use the below right

Thanks very much again

Import-Csv -Path C:\Temp\DisableUsers.csv | ForEach-Object {Remove-ADUser -Identity $_.SamAccountName}

Apologies I read that wrong first time, same one can be used thanks :)

Author

Commented:
Hi OBdA i get following error after running below PS line with user csv (only has samaccount for column) , any ideas on this?

Import-Csv -Path C:\Temp\DisableComputer.csv | ForEach-Object {Get-ADComputer -Identity $_.Name | Remove-ADObject -Recursive -Confirm:$false}

Get-ADComputer : Cannot validate argument on parameter 'Identity'. The argument is null or an element of the argument
ollection contains a null value.
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
If you have the SamAccountName only, and you want to delete a user, you need to use Get-ADUser and the respective column name/property for -Identity:
Import-Csv -Path C:\Temp\DisableUser.csv | ForEach-Object {Get-ADUser -Identity $_.SamAccount | Remove-ADObject -Recursive -Confirm:$false}

Open in new window

Author

Commented:
Thanks getting below error now when running now

Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null or an element of the argument
collection contains a null value.
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
As I said: you need to use the respective column/property from your csv that contains the SamAccountName; I don't know your csv's layout, but you said "only has samaccount for column, so if that's not the column's name, you need to adjust it.
... Get-ADUser -Identity $_.SamAccount ...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial