PowerShell Script to Join AD Domain

PowerShell Script to Join AD Domain

  1. Verify DNS Server is configured correctly
#Verify DNS Server
$DNSServer= "" 

if ((Get-DnsClientServerAddress -InterfaceAlias "Ethernet0" -AddressFamily IPv4).ServerAddresses -eq $DNSServer) {

    Write-Host -ForegroundColor Green "DNS Server is" $DNSServer
else {

    Set-DnsClientServerAddress -InterfaceAlias "Ethernet0" -ServerAddresses $DNSServer

  1. Prepare an AES Key file to encrypt the Password for Domain Administrator and store it on Share Folder.
$KeyFile = "\\UAT-AD01\MyShare\UAT-AES.key"
$PasswordFile = "\\UAT-AD01\MyShare\UAT-Password.txt"

$Key = New-Object Byte[] 32
$Key | out-file $KeyFile

(get-credential).Password | ConvertFrom-SecureString -key (get-content $KeyFile) | set-content $PasswordFile
  1. Enter Username & Password when prompted


  1. Join the server to AD Domain and reboot it
$Password = Get-Content $PasswordFile | ConvertTo-SecureString -Key (Get-Content $KeyFile)
$UserName = "UAT\Administrator" 
$credential = New-Object System.Management.Automation.PsCredential($UserName,$Password)

$DomainName = "uat.aventislab.com"

Add-Computer -computername $env:computername -domainname uat.aventislab.com –credential $credential -Restart -Force

Reference link


Leave a Comment

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

Scroll to Top