Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am putting together a script to run post-sysprep.

It should check if the machine is on the network, and if it is then it should query AD to see if a computer account exists with it's service tag (we're using these as the hostnames of the machines.)

If it does exist, it should delete the account and rejoin the machine to the domain.

I have got the majority of the script running, but need to run the following:

Remove-ADComputer -Identity $distinguishedName

How can I run this from the "standard" powershell environment? I don't want to use the AD module.

(By the way - I'm on a mixed mode 2000/03 domain as we are in the process of upgrading to 2008)

I'm new to PowerShell so be gentle if I'm completely missing the point!



share|improve this question
up vote 3 down vote accepted

DSRM will work fine, too.

However, if you want to stay 100% PowerShell, you can use the ADSI provider to accomplish this natively within PowerShell without modules.

 $computer = "computername"
 $dom = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()            
 $root = $dom.GetDirectoryEntry()            
 $search = [System.DirectoryServices.DirectorySearcher]$root            
 $search.filter = "(&(objectclass=computer)(name=$computer))"            
 $search.findall() | foreach-object{$_.GetDirectoryEntry() } | foreach-object{$_.DeleteObject(0)} 
share|improve this answer
I'd vote for dsrm its less complex! – tony roth May 30 '10 at 6:07

If you want to run a command from the AD module (such as Remove-ADComputer) you are going to have to import that module.

You could try using the dsrm.exe utility to remove the object. See for usage details on dsrm.exe.

Thanks, Mark

share|improve this answer

yea why over complicate things just use dsrm, remeber ps is a a full cli in that you don't have to do anything to get dsrm to work.

so within powershell, it would look like this

ps>dsrm $distinguishedName

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.