PowerShell script – Join a server to a specific OU in Active Directory

A PowerShell script to add a server to a specific OU when joining a domain. If the script fails it will generate an error. The script will prompt for a username and password before it joins.

<s# Join the Domain
Write-Host "Type in 'mylab\username' to join to mylab domain, server will reboot"
$domain = "mylab.local"
Add-Computer -DomainName $domain -Credential $credential -OUPath "OU=Production,OU=My Servers,DC=mylab,DC=local"
if ($?) {
    Write-Host "The last PS command executed successfully" 
	Restart-Computer -Force
pause
} else {
    Write-Host "The last PS command failed"  
pause  
}
This entry was posted in Microsoft, Scripting and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *