Monthly Archive


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"            
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            
Get-QADComputer -Identity $computer |             
Remove-QADObject -WhatIf            
$search = [adsisearcher]$domain            
$search.Filter = "(&(objectclass=computer)(cn=$computer))"            
$search.SizeLimit = 3000            
$search.FindOne() |             
foreach {            
    $comp = $_.GetDirectoryEntry()            

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