PowerShell 2.0: One Cmdlet at a Time 11 Add-Computer
Continuing the series looking at new cmdlets available in PowerShell 2.0. This time we look at the Add-Computer cmdlet.
What can I do with it?
Join a local computer to a domain or workgroup
Example:
Join the current computer to the Test domain, place the computer account in the Servers OU and use the Restart-Computer cmdlet to reboot the computer to complete the process.
Add-Computer -domainname Test -OUPath ‘OU=Servers,DC=test,DC=local’; Restart-Computer
How could I have done this in PowerShell 1.0?
You could have used the Win32_ComputerSystem WMI Class and the JoinDomainOrWorkGroup method.
This script from the Poshcode script repository illustrates how you might use this method to join a computer to a domain.
function Set-Domain { param( [switch]$help, [string]$domain=$(read-host “Please specify the domain to join”), [System.Management.Automation.PSCredential]$credential = $(Get-Credential) ) $usage = “`$cred = get-credential `n” $usage += “Set-AvaDomain -domain corp.avanade.org -credential `$cred`n” if ($help) {Write-Host $usage;exit} $username = $credential.GetNetworkCredential().UserName $password = $credential.GetNetworkCredential().Password $computer = Get-WmiObject Win32_ComputerSystem $computer.JoinDomainOrWorkGroup($domain ,$password, $username, $null, 3)
}
Alternatively you could use the command line tool netdom to join a computer to a domain:
NETDOM /Domain:Test /user:adminuser /password:apassword MEMBER Server01 /JOINDOMAIN