PowerShell: 1-Liner to Change Computer Name and Join Active Directory

# 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

Leave a Reply

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