Windows Autopilot

Correlating Hyper-V VMs that are registered with Windows Autopilot

Over time, I’ve created a whole bunch of VMs for testing Windows Autopilot.  They are named so that I can keep track of the Windows 10 build, the environment they are tied to, the scenario they are configured for, etc.  But overall, it is a still a bit of a mess – cleaning up that mess is my own personal goal (so this in a way is a self-serving post), but in the process of doing that I realized that there is a challenge to overcome.

image

But here’s the challenge:  I would like to delete some of these VMs, while making sure I remove them from Windows Autopilot at the same time.  How do I tie the VM to the Autopilot device registration, where Autopilot shows the serial number of the device and has no idea what the VM name is?  PowerShell to the rescue.  With this query (without the line breaks), I can get all the serial numbers:

Get-WmiObject -ComputerName mdtdev -Namespace root\virtualization\v2 -class Msvm_VirtualSystemSettingData
| ? { $_.VirtualSystemType -eq ‘Microsoft:Hyper-V:System:Realized’} | select elementname, BIOSSerialNumber
| Sort elementName

The output looks like this:

image

Because I need to manually decide what to clean up, I just need to easily be able to see the serial number for each VM, which isn’t displayed in the Hyper-V console.  A simple one-liner can copy the serial number into the “Notes” field for each VM (again, all on one line, remove the line breaks):

Get-WmiObject -ComputerName mdtdev -Namespace root\virtualization\v2 -class Msvm_VirtualSystemSettingData
| ? { $_.VirtualSystemType -eq ‘Microsoft:Hyper-V:System:Realized’} | select elementname, BIOSSerialNumber
| Sort elementName
| % { Set-VM -ComputerName mdtdev -Name $_.elementname -Notes $_.BIOSSerialNumber }

It doesn’t generate any output:

image

But the end result is that I can see the value in the Hyper-V console:

image

If the effort were larger (e.g. I had hundreds of VMs to clean up and not just a few dozen) I could script it even further, but for now I’m happy with a manual approach.

Categories: Windows Autopilot

Tagged as: ,