To change a computer name, use the Rename-Computer cmdlet. Note that the computer must be online and connected to Active Directory.
Basic Syntax
Rename a computer remotely:
Rename-Computer –ComputerName "FS1" -NewName "FS2"
Running Locally
If you want to run this script locally:
Rename-Computer -NewName "newname" -DomainCredential "Domain\Administrator"
Advanced: Rename, Join, and Move to OU
You can improve the renaming script by joining the computer to the domain and putting it into a specified OU simultaneously. The script should be run on the target machine, not on the domain controller.
$NewComputerName = "Server3" # Specify the new computer name
$DC = "contoso.com" # Specify the domain to join
$Path = "OU=TestOU,DC=contoso,DC=com" # Specify the path to the OU
Add-Computer -DomainName $DC -OUPath $Path `
-NewName $NewComputerName –Restart –Force
The script will:
- Prompt for credentials of an account that has permissions to join computers to the domain
- Rename the computer
- Restart it
- Join it to the domain