PowerShell 2.0: One Cmdlet at a Time 77 Remove-Computer

Continuing the series looking at new cmdlets available in PowerShell 2.0. This time we look at the Remove-Computer cmdlet.

What can I do with it?

Remove the local computer from a workgroup or domain.

Example:

Remove the local computer from the current domain, then reboot to make the change take effect using the Restart-Computer cmdlet.

Remove-Computer ; Restart-Computer

How could I have done this in PowerShell 1.0?

You could have used the Win32_ComputerSystem WMIClass and the UnjoinDomainorWorkgroup method. Note: Make sure you run PowerShell with elevated priviliges otherwise it will be unsuccessful and you will receive a return value of 5 rather than 0 for a success.

(Get-WmiObject -Class Win32_ComputerSystem).UnjoinDomainOrWorkgroup($null,$null,0)

Alternatively you could use the command line tool netdom to remove a computer from a domain:

NETDOM remove /userd:adminuser /passwordd:apassword

1000 things 1% better!