Join Computer to Domain with Specified Computer Name and OU

Domain join is a much common task that is performed by system administrators. Since Active Directory is embedded in so many Organizations, moreover every IT professional is familiar with the box below. Assigning the computer name and domain name, requires the delegated domain joining authentication details including user name and password, and a reboot after the above activity. Once you join the system in domain, active directory creates a computer name in default computer location(organizational unit). But Think when you have multiple organizational units on your active directory and want to move the computers from default computer organizational unit to others, then need to move manually. Moving of computers to OU, helps the administrator to apply Group policies on computers and better management.
Powershell provides the ability to join the computer on the desired OU.

Below PowerShell script can help you on multiple levels. and prompt to enter the details:
New Computer Name
Desired OU Name from List by entering the number
Enter Credential details.
Save the PowerShell command with domainjoin.ps1 and execute with PowerShell on the computer which will be joined in the domain. Few Changes are required as per your domain name, organizational unit.

PowerShell command syntax:-

Write-Host “Please enter your desired computer name: [Default $env:computername]:”
$computername = Read-Host
$renamecomputer = $true
if ($computername -eq “” -or $computername -eq $env:computername) { $computername = $env:computername; $renamecomputer = $false }
Write-Host “Please enter your desired location [1-4] [Default 1]:
1. Mumbai
2. Atlanta
3. Singapore
4. Paris”
$ou = Read-Host
$validate = $false
if ($ou -eq “” -or $ou -eq “1”) { $ou = “OU=Computers,OU=Mumbai,DC=wintelpro,DC=local”; $validate = $true }
if ($ou -eq “2”) { $ou = “OU=Computers,OU=Atlanta,DC=your,DC=wintelpro,DC=local”; $validate = $true }
if ($ou -eq “3”) { $ou = “OU=Computers,OU=Singapore,DC=your,DC=wintelpro,DC=local”; $validate = $true }
if ($ou -eq “4”) { $ou = “OU=Computers,OU=Paris,DC=your,DC=wintelpro,DC=local”; $validate = $true }
if ($validate -eq $false) { Write-Host “Invalid input, defaulting to [1].”; $ou = “OU=Computers,OU=Mumbai,DC=wintelpro,DC=local”}
$credentials = New-Object System.Management.Automation.PsCredential(“wintelpro\administrator”, (ConvertTo-SecureString “” -AsPlainText -Force))
Write-Host “Adding $computername to the domain”
Add-Computer -DomainName “” -Credential $credentials -OUPath $ou
if ($renamecomputer -eq $true) { Rename-Computer -NewName $computername -DomainCredential $credentials -Force }

Leave a Reply

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