July 4, 2025

USEFUL-IT

A blog for USEFUL-IT information

Rename the guest OS to match the virtual machine name on Hyper-V

When you are managing a large number of virtual machines, it can get tricky to keep track of everything.  One thing that I see many people doing to help with this is to make sure that the guest operating system has the same network name as the virtual machine name.  This makes life a lot easier when moving between different tools.

However, I tend to rename and copy virtual machines a lot, which makes it difficult to keep the virtual machine and guest operating system name synchronized.  To handle this I wrote the following script:

<#  
.SYNOPSIS  
    Rename the guest OS to match the virtual machine name on Hyper-V  
.DESCRIPTION  
    - Check if the machine model contains "*Virtual Machine*"
    - Read the virtual machine name from the registry inside the virtual machine
    - Clean up the virtual machine name and check it against the guest OS name
    - If they match, move along
    - If they do not match, rename the guest operating system and reboot
.PARAMETER
	No Parameters available
.EXAMPLE
	PS C:\> .\SetGuestNameToHyperVName.ps1
.EXAMPLE
    Use a GPO in your domain to run this script on system startup.
    Everytime a VM is build from this image the systemname will be matched with the name given in Hyper-V
.NOTES  
    File Name  : SetGuestNameToHyperVName.ps1  
    Author     : Wessel van Sandwijk  
    Requires   : Hyper-V, PowerShell V2 or higher
.LINK  
	http://www.useful-it.nl

#>

#----------------------------------------------------
# Create a Source for the Application Log (named after the scriptname)
#----------------------------------------------------
    new-eventlog -logname "Application" -source "$($MyInvocation.MyCommand.Name)" -ErrorAction SilentlyContinue
#----------------------------------------------------------------------------------
#Check if computermodel does not contains "*Virtual Machine*", if yes, exit script
#----------------------------------------------------------------------------------
    if ((Gwmi win32_computersystem).model -notlike "*virtual machine*") {
        Write-EventLog -logname "Application" -source "$($MyInvocation.MyCommand.Name)" -EventId "65000" `
        -EntryType Information -Message "This is not a Virtual Machine`r`n$($MyInvocation.MyCommand.Definition)`r`nScript has not been run";exit}
#----------------------------------------------------------
# Get the virtual machine name from the parent partition
#----------------------------------------------------------
    $vmName = (Get-ItemProperty -path "HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters").VirtualMachineName
#----------------------------------------------------------------
# Replace any non-alphanumeric characters with an underscore
#----------------------------------------------------------------
    $vmName = [Regex]::Replace($vmName,"\W","_")
#-----------------------------------------------
# Trim names that are longer than 15 characters
#-----------------------------------------------
    $vmName = $vmName.Substring(0,[System.Math]::Min(15, $vmName.Length))
#------------------------------------------------------------------
# Check the trimmed and cleaned VM name against the guest OS name
# If it is different, change the guest OS name, write an entry in the application logs and reboot
#------------------------------------------------------------------
    if ($env:computername -ne $vmName) {
        (gwmi win32_computersystem).Rename($vmName)
        Write-EventLog -logname "Application" -source "$($MyInvocation.MyCommand.Name)" -EventId "65001" `
        -EntryType Information -Message "Computer has been renamed from $env:computername to $vmName.`r`n$($MyInvocation.MyCommand.Definition)`r`nReboot Initiated at $(date -f yyyyMMdd/HHmm)"
        shutdown -r -t 0}

What this script does is:

  • Check if the machine model contains “*Virtual Machine*”
  • Writes an event 65000 or 65001 in the Application EventLog
  • Read the virtual machine name from the registry inside the virtual machine
  • Clean up the virtual machine name and check it against the guest OS name
  • If they match, move along
  • If they do not match, rename the guest operating system, write an event in the eventlog and reboot

I have this script inside a GPO as a startup script, configured to run automatically when the machine boots.

 

About The Author