Still confused about the syntax for this. What I'm trying to do is
* remove the workstation from the domain
* delete the computer account from the domain
* join the workstation to the workgroup "Workgroup"
Here's what I've got:

 Code:
$ou = 'OU=MySubOu,OU=MyOU,DC=MyDomain,DC=com'
$user = MyUser
$password = MyPassword
$objNetwork = CreateObject("WScript.Network")
$strComputer = $objNetwork.ComputerName
$objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\.\root\cimv2:Win32_ComputerSystem.Name='"+@wksta+"'")
$ReturnValue = $objComputer.UnJoinDomainOrWorkGroup("MyDomain.com", $password, "MyDomain\"+$user, $OU, 2)
If $ReturnValue
	? "Failed removing "+@WKSTA+" from domain"
	? "ReturnValue = " + $ReturnValue
		GoTo END
EndIf
$ReturnValue = $objComputer.JoinDomainOrWorkGroup("Workgroup")
If $ReturnValue
	? "Failed joining "+@WKSTA+" to Workgroup"
	? "ReturnValue = " + $ReturnValue
		GoTo END


When I run this, I get no return value from the UnJoinDomainOrWorkGroup. But I get return value 2691 for the JoinDomainOrWorkGroup, which is still "The machine is already joined to the domain."

Despite the first part not generating a return value, making it look like it was successful, the computer properties still show it being a member of the domain, and the computer account is still in the domain. This obviously is not doing anything.

Has anyone ever done what I'm trying to do, successfully?