PowerShell Command to join computer to domain

 It would be very easy to join a computer to the domain via PowerShell.

You can replace the values to following PowerShell block according to your domain environment.

$dc = "domain.local"
$usr = "$dc\Administrator"
$pw = "T0p$3cret123" | ConvertTo-SecureString -asPlainText –Force
$creds = New-Object System.Management.Automation.PSCredential($usr,$pw)
$old_comp = Comp123
$comp = Comp456
Add-Computer -DomainName $dc -ComputerName $old_comp -newname $comp -Credential $creds -restart -force -verbose

First, we defined what is domain name we want to set on the computer. Above value is based on assumption that the domain name is "domain.local".

Secondly, we defined user who has rights to join the domain. Above value is set to Domain Administrator user "Administrator" and following by that user's password defined in quote.

Thirdly, we will pass secure string and username in to forth line to create credential object. 

Note: The fifth and sixth lines are optional. It is needed when you have different current computer name and you want to change the computer name along with the domain joining. If you don't want to change the computer name, just remove the "-ComputerName $old_comp -newname $comp" parameters. 

Finally, last line is main command which uses all the values we defined and tries to join computer to the domain. If all details are correct, computer will be joined to the domain and gets restarted.

If you want to put that computer in specific Organizational Unit (OU), you can add following parameter to the final command by replacing details matching with your scenario:

-OUPath "OU=Computers,OU=East,DC=domain,DC=local"

Example: 
Add-Computer -DomainName $dc -ComputerName $old_comp -newname $comp -OUPath "OU=Computers,OU=East,DC=domain,DC=local" -Credential $creds -restart -force -verbose

That's all. Hope you would find it helpful. Thank you.  

Comments