Join Computer to Domain with Desired Computer Name and OU

May 12th, 2022 | Tags: ,

Step #1: This is the simplest method to add a computer to a domain. In this example you will be prompted for credentials followed by the required reboot.

Add-Computer -DomainName "your.domain.here"
Restart-Computer

Step #2: If you require an automated script without prompting the user for credentials you can provide the user account with rights to add computers to the domain.

$cred = New-Object System.Management.Automation.PsCredential("domain\useraccountwithjoinpermissions", (ConvertTo-SecureString "useraccountpassword" -AsPlainText -Force))
Add-Computer -DomainName "your.domain.here" -Credential $cred
Restart-Computer

Step #3: The following example shows how to specify the Organizational Unit in Active Directory where the computer account will reside.

$cred = New-Object System.Management.Automation.PsCredential("domain\useraccountwithjoinpermissions", (ConvertTo-SecureString "useraccountpassword" -AsPlainText -Force))
Add-Computer -DomainName "your.domain.here" -Credential $cred -OUPath "OU=computers,OU=yourlocation,DC=your,DC=domain,DC=here"
Restart-Computer

Step #4: The following example highlights how you can add a computer to the domain as a new computer name without a reboot in between. Imaging processes may initially assign your computer a random name that requires changing to include a specific naming standard.

$cred = New-Object System.Management.Automation.PsCredential("domain\useraccountwithjoinpermissions", (ConvertTo-SecureString "useraccountpassword" -AsPlainText -Force))
Add-Computer -DomainName "your.domain.here" -Credential $cred -OUPath "OU=Computers,OU=YourLocation,DC=your,DC=domain,DC=here"
Rename-Computer -NewName $newcomputername -DomainCredential $credential -Force
Restart-Computer

Step #5: The final example allows for input from the user to determine both the computer name and the Active Directory location. This utilizes Write-Host and Read-Host with a small amount of IF THEN validation to accomplish this. If someone does not enter any value it will use the Default value as displayed to the user. For the numbers 1-4 option if an invalid character is entered it will also use the specified Default setting.

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. Chicago
2. Paris
3. Sydney
4. Toronto"
$ou = Read-Host

$validate = $false
if ($ou -eq "" -or $ou -eq "1") { $ou = "OU=Computers,OU=Chicago,DC=your,DC=domain,DC=here"; $validate = $true }
if ($ou -eq "2") { $ou = "OU=Computers,OU=Paris,DC=your,DC=domain,DC=here"; $validate = $true }
if ($ou -eq "3") { $ou = "OU=Computers,OU=Sydney,DC=your,DC=domain,DC=here"; $validate = $true }
if ($ou -eq "4") { $ou = "OU=Computers,OU=Toronto,DC=your,DC=domain,DC=here"; $validate = $true }
if ($validate -eq $false) { Write-Host "Invalid input, defaulting to [1]."; $ou = "OU=Computers,OU=Chicago,DC=your,DC=domain,DC=here"}

$credentials = New-Object System.Management.Automation.PsCredential("yourdomain\useraccountwithjoinpermissions", (ConvertTo-SecureString "useraccountpassword" -AsPlainText -Force))
Write-Host "Adding $computername to the domain"
Add-Computer -DomainName "your.domain.here" -Credential $credentials -OUPath $ou
if ($renamecomputer -eq $true) { Rename-Computer -NewName $computername -DomainCredential $credentials -Force }
Restart-Computer

join-domain

Step #6: The following is a Function example that can be used within a script or from a GUI compliments from a reader.

function Join-Domain {

Param(
[Parameter(Position=0)]
[String]$computername = $env:computername,
[Parameter(Position=1)]
[ValidateSet(“OU=Computers,OU=Chicago,DC=your,DC=domain,DC=here”,”OU=Computers,OU=Paris,DC=your,DC=domain,DC=here”,”OU=Computers,OU=Sydney,DC=your,DC=domain,DC=here”,”OU=Computers,OU=Toronto,DC=your,DC=domain,DC=here”)]
[string]$OU = “CN=Computers,OU=Chicago,DC=your,DC=domain,DC=here”
)
$renamecomputer = $true
if ($computername -eq “” -or $computername -eq $env:computername) { $computername = $env:computername; $renamecomputer = $false }

$credentials = New-Object System.Management.Automation.PsCredential(“yourdomain\useraccountwithjoinpermissions”, (ConvertTo-SecureString “useraccountpassword” -AsPlainText -Force))
Write-Verbose “Adding $computername to the domain under $OU”
Add-Computer -DomainName “your.domain.here” -Credential $credentials -OUPath $OU
if ($renamecomputer -eq $true) { Rename-Computer -NewName $computername -DomainCredential $credentials -Force }
}
No comments yet.