Duy Gonzales
Duy Gonzales

Reputation: 53

Rename computer and add join domain powershell script

I am trying to rename a machine and add it to the domain with 1 restart from WORKGROUP (after the machine gets renamed and joined domain). I tried the code below but it's giving me an error:

$bios = (Get-WmiObject Win32_Bios).SerialNumber
$name = $bios
Rename-Computer -NewName "$name"
$domain = "DOMAINNAME"
$username = "USERNAME"
$password = "PASSWORD" | ConvertTo-SecureString -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential($username,$password)
Add-Computer -DomainName $domain -Credential $credential -NewName $name
Read-Host "The computer will restart in 5..."
shutdown /r /t 5

The error I'm receiving is the account already exist. It does join the domain but not the "Renamed" name that I want.

Upvotes: 2

Views: 5454

Answers (1)

Prasoon Karunan V
Prasoon Karunan V

Reputation: 3043

This is how it has to be done.

Rename-Computer -NewName newserver -Force
Add-Computer -DomainName example.ne -Credential $credential -NewName newserver -Options JoinWithNewName

You could better read the full documentation of Add-Computer cmdelt. Get-Help Add-Computer -Online

Upvotes: 3

Related Questions