Posted On July 7, 2020

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

kimconnect 2 comments
blog.KimConnect.com >> Codes >> 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

2 thoughts on “PowerShell: 1-Liner to Change Computer Name and Join Active Directory”

  • If I enter -DomainName after Add-Computer I get the error – A positional parameter cannot be found that accepts the argument “-”
    If I enter – DomainName after Add-Computer, I get the error – A positional parameter cannot be found that accepts the argument “DomainName”
    If I enter DomainName after Add-Computer, I get the error – A positional parameter cannot be found that accepts the argument

    In the first option, I put no space between the hyphen and ‘DomainName’
    In the second option I put a space between the hyphen and ‘DomainName’
    In the third option I don’t use a hyphen at all

    What am I doing wrong?

    • A space is required between each ‘arguments’ of a cmdlet.
      -DomainName must be followed by a valid that represents FQDN of your domain name
      the switch -DomainName must be specified before inputting the actual
      representing the FQDN

Leave a Reply

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

Related Post

PowerShell: Run PowerShell As Another User

# Set credential $adminUsername='domain\adminDude' $adminPassword='whatpassword?' $GLOBAL:adminCredential=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminUsername,$(ConvertTo-securestring $adminPassword -AsPlainText -Force) # Test…

Error: 502 Gateway Error

Explanation: 502 Gateway Error generally means connection problems with an upstream proxy. In this instance,…

PowerShell: Get Size On Disk and Discover Largest Files in a Given Directory

# getSizeOnDisk-v0.01.ps1# Set variables$parentDirectory="C:\"$depth=4$topX=10$excludeDirectories="C:\Windows","C:\Program Files","C:\Program Files (x86)"$clusterSizeOfNetworkShare=8192# sanitateif($depth -le 1){$depth=1}################################## Excuting Program as an Administrator…