09 April 2015

Find duplicate VMs in your SCVMM environement

Sometimes there is a reason for legitimately having duplicate VM names that would not cause a problem at all.  If you have a replicated VM you would have two, one on the other off.

You could also have a VM stored in the library that is the same name as a server that is currently deployed.

Then there are the duplicate VMs that your really want to find and get rid of.  These can be cause by a number of factors, most of them human.

To track down duplicates use the following

Get-SCVirtualMachine | group -Property name |? {$_.Count -gt 1} | % {$_.Group} | Select-Object Name,virtualmachinestate,IsPrimaryVM, IsRecoveryVM,ID  ,hostname,HostGrouppath  | Format-Table -AutoSize

It will give you an output similar to this

The green machines are an actual replicated pair and is not a problem.
The amber machines is a duplicate we would like to get rid of.

The command will also give you the unique ID.  You can remove the unwanted duplicate using the following

Get-SCVirtualMachine -ID "****copyfromoutput****" | Remove-SCVirtualMachine -Force