Incorrectly named VMs

I was recently contacted via my blog and asked if there was a way to find the VMs which are not named in vCenter the same as they are in Active Directory or their hostname, the person contacting me had explained that someone had “accidently” renamed a number of VMs and when there was an issue with the windows VM they were unable to find it as the name of the VM did not relate to the hostname in any way.

The quick way to find the VM was easy enough, we can easily find the VM with this single one-liner:

Get-VM | Where { $_.Guest.HostName -match "HostnameOfVM"} | Select Name, @{N="Hostname";E={$_.Guest.Hostname}}

If you replace “HostnameOfVM” in the above example with the hostname you should get the correct results back showing the name and the Hostname of the VM.

With this in mind I expanded the above one-liner a little to check your vCenter for each VM which may have a VM name which is different to the hostname, now obviously this depends on if you name the VMs with the hostname or the FQDN so I added an option for both.

If you name your VM’s with the FQDN like MyVM.MyDomain.Com rather than just the hostname (MyVM) then simply set the $IncludeFQDN to $true rather than the preset $false.

As it stands at the moment, the below script will tell you about the VMs which are not named correctly and it will also tell you that it would have changed the name, that’s because this script is currently in “Whatif” mode, this mode means it will tell you if it was going to make a change and not actually make the change.  I would run the script as it was and if you are happy with the results simply remove the –whatif which is located after the Set-VM cmdlet, this will then enable the cmdlet to set the name of the VM back to the correct name.

Connect-VIServer MyvCenterSRV
# Specify if your virtual machine names include the FQDN
$IncludeFQDN = $false

Get-VM | Where { $_.PowerState -eq "PoweredOn" } | Foreach {
    If ($IncludeFQDN) {
		$Name = $_.Guest.Hostname
	} Else {
		$Name = (([string]$_.Guest.HostName).Split("."))[0]
    If ($_.Name -ne $Name) {
        If ($_.Guest.Hostname) {
            Write "VM name '$($_.Name)' is not the same as the hostname $Name"
			Set-VM $_.Name -Name $Name -whatif
        } Else {
            Write "Unable to read hostname for $($_.Name) - No VMTools ?"

A gentle reminder that whilst this may rename the VM name correctly it will obviously not touch the name of the folder on the datastore, if the name was not correct in the first place then your folder name will be incorrect which is an entirely different issue (picked up by my vCheck script), this can of course be resolved whilst the VM is powered on by performing a storage vmotion to another datastore, a SVMotion renames the folder and files to the correctly named entities.

4 thoughts on “Incorrectly named VMs

  1. Steve

    How can this be setup to recursively change all VMs without having to press Y at the prompts for each VM?

  2. Virtu-Al

    Nice one, didn’t see your script there, a comment from Jeffrey too, big time baby !

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.