Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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!

Thanks,

Ben

share|improve this question

3 Answers 3

up vote 2 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 http://technet.microsoft.com/en-us/library/cc755841(WS.10).aspx 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

 
discard

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.