I am pretty green at Powershell and if this is simple, I apologize. I have tried to use this example script from user Nick, and run into an error I thought AdObject was supposed to go around. My Script
Import-Module ActiveDirectory
$list = Import-CSV C:\scripts\deletebulkusers.csv
forEach ($item in $list) {
$samAccountName = $item.samAccountName
#Get DistinguishedName from SamAccountName
$DN = Get-ADuser -Identity $Samaccountname -Properties DistinguishedName |
Select-Object -ExpandProperty DistinguishedName
#Remove object using DN
Remove-ADObject -Identity $DN
}
I have posted my error below:
Remove-ADObject : The directory service can perform the requested operation only on a leaf object
At line:13 char:5
+ Remove-ADObject -Identity $DN -confirm:$false
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (CN=<user>,DC=com:ADObject) [Remove-ADObject], ADException
+ FullyQualifiedErrorId : ActiveDirectoryServer:8213,Microsoft.ActiveDirectory.Management.Commands.RemoveADObject
Glad I could help :) I've had this happen to me before, specifically if the user has Outlook on their phone it turns them into a container. If you open Active Directory Users and Computers, select "View", and check the option for "View Users, Contacts, Groups and Computers as containers" the navigate to the object, you will see that there are items inside of it.
Using the -recurse
parameter with Remove-ADObject
is what fixed it for me.