I have been trying to use the Add-Computer Cmdlet to take and newly generated Windows 2019 and 2016 EC2 Instance, which starts with a randomly generated hostname, and set the hostname, and join it to the domain. So far, I have not been able to get this to work without having two steps and two reboots. I have to Rename-Computer, reboot, and Add-Computer and reboot.
I have tried so many things, I have lost track, but here is one example:
Add-Computer -DomainName $domain -OUPath $ouPath -Credential $credential -NewName $($nameValue.Split(".")) -Force –Options JoinWithNewName,AccountCreate -ErrorAction Stop -Restart\n