Rename server name if server has not yet joined AD
$Hostname = "newname"
$username = "Administrator"
$Password = "xxxxxxxx"
$pwd = $Password | ConvertTo-SecureString -asPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($UserName,$pwd)
Rename-Computer -NewName $Hostname -LocalCredential $credential -Restart
Rename server name if server has already joined AD
$Hostname = "newname"
$username = "domainadmin"
$Password = "xxxxxxxx"
$pwd = $Password | ConvertTo-SecureString -asPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($UserName,$pwd)
Rename-Computer -NewName $Hostname -DomainCredential $credential –Restart