Some cool cmdlets that can help you with migrating or provisioning computers.
- By Jeffery Hicks
Windows PowerShell 2.0 has a couple of handy cmdlets for managing a computer's domain membership. These might come in handy if you are developing a PowerShell provisioning or migration script. In the past we would have used the command line tool NETDOM.EXE to join and dis-join a computer from a domain. In PowerShell we can achieve similar functionality with Add-Computer and Remove-Computer.
Add-Computer is run locally and requires the domain name. Although not required, more than likely you will also need to specify a credential with permission to add the computer to the domain. Otherwise the cmdlet uses the current user credential. Here's the most basic example:
PS C:\> add-computer mycompany -cred mycompany\administrator
You will be prompted for the administrator password. If a computer account does not exist in the domain, it will be created in the default Computers container. Or you can use the -OUPath parameter to specify a location:
PS C:\> add-computer mycompany -cred mycompany\administrator -OUPath "OU=Sales,OU=Desktops,DC=MyCompany,DC=local" -passthru
I suggest using the -passthru parameter which will display a result summary.
Remove-Computer dis-joins the local computer from the domain and disables the computer account in Active Directory. You must also specify credentials, regardless of whether the current user already has permission.
Use the -Verbose and -Passthru parameters to view the results:
PS C:\> remove-computer -cred mycompany\administrator -verbose -passthru
Of course, neither of these actions is complete until the computer is rebooted. But don't worry, there's a new cmdlet for that:
PS C:\> restart-computer -force
The -Force parameter is optional but will force an immediate shutdown instead of waiting the typical 30 seconds. This cmdlet, by the way, can also be used to restart remote computers. Take a look at cmdlet help and examples for more information. Or if you prefer to simply shut the computer down, you can use Stop-Computer:
PS C:\> stop-computer -force
This cmdlet also can shut down remote machines.
Finally, this is all serious stuff so please test these cmdlets in a non-production setting before you start going wild with them.
About the Author
Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.