# The 1-liner
Add-Computer -DomainName 'somedomain.com' -credential kimconnect.com\domainadmin1 -ComputerName $env:computernname -newname 'NewName' -restart
# Quick Snippet
$domain='kimconnect.net'
$joinName='NEW-BOX'
$adminUser='AdminDude'
$adminPass='WhatPassword?'
$credential=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminUser,$(ConvertTo-securestring $adminPass -AsPlainText -Force)
 
function addComputerToDomain($domainName,$joinName,$adminCred){
Add-Computer -DomainName $domain -ComputerName $env:computernname -newname $joinName -Credential $adminCred -restart
}
 
addComputerToDomain $domain $joinName $credential
# Troubleshooting:
# Problem
#add-computer : Computer 'WIN-F61FJS6DTM3' was successfully joined to the new domain 'kimconnect.net',
#but renaming it to 'TEST-BOX' failed with the following error message : The directory service is busy.
#
# Solution:
# Change computername:
Rename-Computer -NewName 'NEW-BOX' -Restart