Menu Close

Read Hyper-V virtual machine name in guest OS for renaming

Home lab in Hyper-V

For testing purposes I run several servers in my home lab, physical and virtualized. One of my servers is a Server 2019 with Hyper-V enabled. In this server, with a Xeon E3-1246 v3 (4 cores, 8 threads) 32 GB ram and some SSD’s, there’s enough testing performance.

Once in a while I recreate my servers or create a single test machine. When manually creating a virtual machine in Hyper-V the ‘Name’ of the virtual machine is set during the ‘New server’ wizard, i.e. ‘LAB01‘.

During a manual installation of the O.S. a random name is generated. If you want the same name in the O.S. as the host name we have to do some renaming.

The host name is visible in the guest O.S.

Some of the information of the host is visible in the registry of the guest O.S. In this case I used Server 2019:

The HostName is the name of my physical server. And the VirtualMachineName (in this example ‘LAB01’) is the name as shown as the virtual machine name in Hyper-V, and this is the value I want to use as my ‘Computer name’!

Rename with PowerShell

While it’s a quick task to manually put in the desired hostname and reboot the machine, what fun is that.. We can do this in PowerShell because the values are present in the registry:

Conclusion

This works with Server 2019. Server 2022 has a different structure in the registry.

Leave a Reply

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