Join Computer to Domain with Specified Computer Name and desired OU

Domain join is a common task that is performed by system administrators. 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).

Powershell provides the ability to join the computer on the desired OU. Moving of computers to OU, helps the administrator to apply group policies on computers and better management

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 Local administrative rights to run the powershell command on the computer which will be joined in the domain. You need to enter the desired details required as per your domain name, desired OU and domain joining delegated account and credentials

Power Shell 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 }

After rebooting of machine you can verify the domain joining status of the computer and active directory organizational unit which you have selected

Post A Comment

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