Remove-ADComputer after retrieving DN's

I have all the DN’s of computer objects I want to delete from AD:

$comp = Import-Csv C:\temp\XP_disable\computers.csv
$comp | ForEach-Object {
    Get-ADComputer -Identity $_.Name -Properties * |
    select DistinguishedName
} | Export-Csv C:\temp\XP_disable\XP_DN.csv -NoTypeInformation

How do get this to delete them now?

$comp | ForEach-Object {
    Remove-ADComputer $_.DistinguishedName


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

Hey Jeff,
First of all, I don’t think the foreach-object should be necessary in this instance.
However, the error suggests that the computer(s) in question are not the final object in the tree. In other words, they may have other objects inside them.

If this is the case you could try Remove-ADObject with the -recursive parameter
More about this cmdlet here:


Also check to see if the computer objects are protected from accidental deletion - that will stop you deleting them until its removed

$comp = Import-Csv C:\temp\XP_disable\computers.csv
$comp | ForEach-Object {
    Get-ADComputer -Identity $_.Name | Remove-ADComputer -Confirm:$false

Does that not solve what you need to solve? I don’t see the need to create a second CSV, and import the second CSV, and then do your deletes. Just do it all on the same pipeline. Unless I’m missing some critical step that happens in between the two, but I don’t think I am.

Thanks Liam, -Recursive param worked.

thanks Stephen…the -Confirm:$false was also exactly what I was looking for. Yes, piping inside the one-liner makes more sense, appreciate it.