Script to change the first 3 characters of a hostname – PowerShell

We were failing over a bunch of Windows Virtual machines to a new site and they all needed to be renamed to match their new location. The following script will rename the first 3 characters of the hostname from DC1 to VC5, the rest will remain the same.


Save the script as ChangeHostname.ps1, adjust second line and run.

$DRHostName = $env:computername
$DRHostName = $DRHostName -replace "DC1", "VC5"
$DAdminUsername = "Administrator"
$secpasswd = Read-Host -Prompt "Enter password for local $DAdminUsername, system will reboot" -AsSecureString 
$credential = New-Object -TypeName System.Management.Automation.PSCredential($DAdminUsername, $secpasswd)
Rename-Computer -NewName  $DRHostName -DomainCredential $credential -Restart -Force
This entry was posted in Microsoft, Scripting and tagged , . Bookmark the permalink.

Leave a Reply

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