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:

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.


Updated: December 20, 2015 — 22:31