Remove-ADComputer after retrieving DN's

This topic contains 5 replies, has 4 voices, and was last updated by  Jeff Taylor 1 year, 1 month ago.

  • Author
  • #72254

    Jeff Taylor

    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
  • #72263

    Liam Kemp

    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:


    • #72316

      Jeff Taylor

      Thanks Liam, -Recursive param worked.

  • #72278

    Richard Siddaway

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

  • #72305

    Stephen Valdinger
    $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.

    • #72317

      Jeff Taylor

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

You must be logged in to reply to this topic.