Categories

Delete a computer object

I’m cleaning out a few virtual machines – deleting the Windows 8 developer previews now I’ve got the beta VMs created.  Also means I need to clean up AD so its a good opportunity to look at deleting computer objects

$dom = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()            
$domain = $dom.GetDirectoryEntry()            
            
$computer = "server8build"            
            
"`nMicrosoft"            
Get-ADComputer -Identity $computer |             
Remove-ADComputer -WhatIf            
            
"`nAD provider"            
Get-ChildItem -Filter "(&(objectclass=computer)(cn=$computer))" `
-Path Ad:\$($domain.distinguishedname) -Recurse  |             
foreach {            
  Remove-Item -Path AD:\$($_.distinguishedname) -WhatIf -Force -Recurse            
}            
            
"`nQuest"            
Get-QADComputer -Identity $computer |             
Remove-QADObject -WhatIf            
            
"`nScript"            
$search = [adsisearcher]$domain            
$search.Filter = "(&(objectclass=computer)(cn=$computer))"            
$search.SizeLimit = 3000            
            
$search.FindOne() |             
foreach {            
    $comp = $_.GetDirectoryEntry()            
    $comp.DeleteObject(0)            
}


I have used –WhatIf on the first three options so I can use the same object



The cmdlets work on a get | remove model but note we have a Remove-ADComputer but have to use the more generic Remove-QADObject



The provider discovers the computer and then pipes it into a foreach for Remove-Item to work



The script does a search and then gets the directory entry object. The DeleteObject() method exterminates the computer object.



Take the –WhatIf off the example you need to use

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>