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

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
LVL 1
Indie101Asked:
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.

MaheshArchitectCommented:
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

oBdACommented:
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

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
Indie101Author Commented:
Thanks get below error when I use that

Remove-ADComputer : A parameter cannot be found that matches parameter name 'Force'.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Indie101Author Commented:
Thanks oBDA the columns are "Name","DNSHostName","LastLogondate","DistinguishedName" will it work with this?
oBdACommented:
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

Indie101Author Commented:
Thanks oBdA the first one worked fine, looking for one for the user accounts now if possible
oBdACommented:
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

Indie101Author 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 :)
Indie101Author 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.
oBdACommented:
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

Indie101Author 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.
oBdACommented:
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 ...
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.