Remove-ADComputer after retrieving DN's

This topic contains 5 replies, has 4 voices, and was last updated by Profile photo of Jeff Taylor Jeff Taylor 2 weeks, 2 days ago.

  • Author
    Posts
  • #72254
    Profile photo of Jeff Taylor
    Jeff Taylor
    Participant

    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
    }

    ERROR:

    Remove-ADComputer : The directory service can perform the requested operation only on a leaf object
  • #72263
    Profile photo of Liam Kemp
    Liam Kemp
    Participant

    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:

    https://technet.microsoft.com/en-us/library/ee617219.aspx

    Liam

    • #72316
      Profile photo of Jeff Taylor
      Jeff Taylor
      Participant

      Thanks Liam, -Recursive param worked.

  • #72278
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

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

  • #72305
    Profile photo of Stephen Valdinger
    Stephen Valdinger
    Participant
    $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
      Profile photo of Jeff Taylor
      Jeff Taylor
      Participant

      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.