How to Set Up Hyper-V VM Groups with PowerShell

Save to My DOJO

How to Set Up Hyper-V VM Groups with PowerShell

The other day I was poking around the Hyper-V PowerShell module and I came across a few commands that I must have initially ignored. I had no idea this feature existed until I came across the cmdlets. I can’t find anything in the Hyper-V Manager console that exposes this feature as well so if you want to take advantage of it, PowerShell is the way to go. It turns out that you can organize your virtual machines into groups with these commands.

VM Group cmdlets in PowerShell

NOTE: You should take the time to read through full help and examples before using any of these commands.

Creating a VM Group

The commands for working with VM Groups support remote connections and credentials. The default is the local host and take note that you can only specify credentials for remote connections. Creating a new group is a pretty simple matter. All you need is a group name and type.  You can create a group that is a collection of virtual machines (VMCollectionType) or a group that is a collection of other groups (ManagementCollection). I’m going to create a group for virtual machines.

$VMHost = "ThinkP1"
New-VMGroup -Name Lnx -GroupType VMCollectionType -ComputerName $VMHost

Creating a new VM group

I’ve created the group and can retrieve it with Get-VMGroup.

Retrieving a VM Group with PowerShell

You can only create one group at a time, but you can create the same group on multiple servers.

New-VMGroup Master -GroupType ManagementCollectionType -ComputerName $vmhost,bovine320

This command created the management group Master on both Hyper-V hosts.

Adding a Group Member

Adding members to a group requires a separate step but isn’t especially difficult. To add members to a VMCollectionType group, you need references to the virtual machine object.

$mem = Get-VM fedora28 -ComputerName $VMhost
Add-VMGroupMember -Name Lnx -VM $mem -ComputerName $VMHost

The command won’t write anything to the pipeline unless you use -Passthru. You can take advantage of nested expressions and create a group with members all in one line.

Add-VMGroupMember -VMGroup (New-VMGroup -Name Win -GroupType VMCollectionType -ComputerName $VMHost) -VM (Get-VM Dom1,Srv1,Srv2,Srv3 -ComputerName $VMHost) -Passthru

With this one-line command, I created another group call Win and added a few virtual machines to the group.

Creating a VM Group and Members in a PowerShell one-liner

Since I have two groups, let me add them to the management group.

Add-VMGroupMember -Name Master -VMGroupMember (Get-VMGroup lnx,win -computername $vmhost) -computername $vmhost -Passthru

Adding VM Management Groups with PowerShell

And yes, you can put a virtual machine in more than one group.

Retrieving Groups and Group Members

Using Get-VMGroup is pretty straightforward. Although once you understand the object output you can customize it.

Get-VMGroup -ComputerName $VMhost | 
Select-Object Computername,Name,GroupType,
@{Name="GroupCount";Expression={$_.vmgroupmembers.count}} |

Enumerating VM Groups

Depending on the group type you will have a nested collection of objects. You can easily enumerate them using dotted object notation.

Listing VM Group virtual machines

You can do something similar with management groups.

Expanding VM management groups with PowerShell

Be aware that it is possible to have nested management groups which might make unrolling things to get to the underlying virtual machines a bit tricky. I would suggest restraint until you fully understand how VM groups work and how you intend to take advantage of them.

The output of the VMMembers property is the same virtual machine object you would get using Get-VM so you can pipe this to other commands.

Incorporating VM Groups into a PowerShell expression

Group membership can also be discovered from the virtual machine.

Get-VM dom1,srv1,srv2 -ComputerName $vmhost | Select-Object Name,Groups,Computername

Listing groups from the virtual machine

You cannot manage group membership from the virtual machine itself.

To remove groups and members, the corresponding cmdlets should be self-evident and follow the same patterns I demonstrated for creating VM groups and adding members.

Potential Obstacles

When you assign a virtual machine to a group, the membership is linked to the virtual machine. This may pose a challenge down the road. I haven’t setup replication with a virtual machine that belongs to a group so I’m not sure what the effect if any, might be. But I do know that if you export a virtual machine that belongs to a group and import the virtual machine on a different Hyper-V host, you’ll encounter an error about the missing group. You can remove the group membership on import so it isn’t that much of a showstopper. Still, you might want to remove the virtual machine from any groups prior to exporting.

Doing More

The VM group cmdlets are very useful, but not useful enough. At least for me. I have a set of expectations for using these groups and the cmdlets as written don’t meet those expectations. Fortunately, I can write a few PowerShell functions to get the job done. Next time, I’ll share the tools I’m building around these commands.

In the meantime, I hope you’ll share how you think you’ll take advantage of this feature!

Want to boost your Hyper-V performance? Discover 6 Hardware Tweaks that will Skyrocket your Hyper-V Performance

Threat Monitor
Share this post

Not a DOJO Member yet?

Join thousands of other IT pros and receive a weekly roundup email with the latest content & updates!

59 thoughts on "How to Set Up Hyper-V VM Groups with PowerShell"

  • Adam Kessler says:

    OK, so you’ve showed how to create these groups. The question is, what is the value of these kinds of groups. What does it mean in a practical sense and how do you use them?

  • Jan says:

    Hey Jeffery!

    Thanks for this post and the follow-up. I find the groups promising but I’m unsure how the groups behave in a clustered environment. As Long as I use “-ComputerName ” with all PowerShell Commands everything seems fine. But I did not find any documentation what happens with the groups if I will join a new host to an existing cluster with existing groups.

    Do you now if there is some kind of “replication” of the groups in the cluster?

    Best regards,

Leave a comment or ask a question

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

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

Notify me of follow-up replies via email

Yes, I would like to receive new blog posts by email

What is the color of grass?

Please note: If you’re not already a member on the Dojo Forums you will create a new account and receive an activation email.