The following script can be used to domain join a PC.
# This script will join a PC to the domain. The PC name will be configured based on the hardware serial number.
# The PC must not already be a member of the domain before running this.
#Requires -RunAsAdministrator
# The credentials used to join the PC to the domain
$credential = Get-Credential
# To make this script unattended the below lines can be used instead of the above Get-Credential
#$username = "Username Here"
#$password = ConvertTo-SecureString "Password Here" -asplaintext -force
#$credential = New-Object System.Management.Automation.PSCredential $username,$password
# The domain to join
$domain = 'my.domain.com'
# The OU to put the PC object in when joining
$ou = 'OU=Computers,DC=my,DC=domain,DC=com'
# Prefix the PC name with these characters
$prefix = 'PC'
# Generate random characters for padding to make sure the PC name is 15 characters
$pad = (-join ((48..57) + (97..122) | Get-Random -Count 15 | ForEach-Object {[char]$_})).ToUpper()
# Check PC is not currently on the domain
Write-Host -ForegroundColor Gray 'Checking current domain membership...'
try {
$current = (Get-WmiObject -Class Win32_ComputerSystem -ErrorAction Stop).Domain
Write-Host -ForegroundColor Gray "Currently a member of domain $current"
}
catch {
$message = $_.Exception.message
Write-Error 'Error getting domain information from WMI'
throw $message
}
if ( $domain -eq $current ) {
Write-Error "PC is already a member of $domain - cannot join the same domain again"
exit
}
# First attempt to get the serial number of the hardware
Write-Host -ForegroundColor Gray 'Generating name for PC...'
if ( (Get-WmiObject win32_SystemEnclosure).SerialNumber ) {
# Convert serial to uppercase
$serial = (Get-WmiObject win32_SystemEnclosure).SerialNumber.ToUpper()
Write-Host -ForegroundColor Gray "Got hardware serial number $serial"
# If serial is more than 8 characters truncate the length
if ($serial.length -gt 8) {
$serial = $serial.substring(0, [System.Math]::Min(8, $serial.Length))
Write-Host -ForegroundColor Gray "Serial number has been truncated to $serial"
}
} else {
# No serial retrieved, create a random 8 character string
$serial = (-join ((48..57) + (97..122) | Get-Random -Count 8 | ForEach-Object {[char]$_})).ToUpper()
Write-Host -ForegroundColor Gray "Could not get hardware serial number; a random serial has been generated: $serial"
}
# Build PC name and truncate to exactly 15 characters
$name = "$prefix-$serial-$pad"
$name = $name.substring(0, [System.Math]::Min(15, $name.Length))
Write-Host -ForegroundColor Gray "PC name generated: $name"
# Set the new computer name
Write-Host -ForegroundColor Yellow "Changing PC name to $name..."
try {
Rename-Computer -NewName $name -ErrorAction Stop
}
catch {
$message = $_.Exception.message
Write-Error 'Error changing PC name:'
throw $message
}
Write-Host -ForegroundColor Green "PC name is now $name (a reboot is required to activate this)"
# Join the domain
Write-Host -ForegroundColor Yellow "Joining domain $domain..."
try {
Add-Computer -Credential $credential -DomainName $domain -OUPath $ou -Options JoinWithNewName,AccountCreate -Force -ErrorAction Stop
}
catch {
$message = $_.Exception.message
Write-Error 'Error joining the domain:'
throw $message
}
Write-Host -ForegroundColor Green "PC has been joined to the domain. Please reboot now."
The following variables must be updated to suite your environment:
$domain
: The domain name to join$ou
: The OU to place the computer object in$prefix
: Prefix the PC name with these characters