Get started Bring yourself up to speed with our introductory content.

Hyper-V PowerShell and ForEach loops save time, boost power

Not all cmdlets are made alike: Here's a how-to guide to reduce administrative overhead and get the most of out of Hyper-V with PowerShell and ForEach loops.

Hyper-V PowerShell cmdlets can reduce the time it takes to perform a task -- especially repetitive jobs. PowerShell...

cmdlets can also eliminate the need for using the Hyper-V Manager graphical user interface for daily operational tasks. While there are many Hyper-V PowerShell cmdlets available, not all cmdlets are useful for daily management tasks. Let's look at some of the most commonly used PowerShell cmdlets and see how adding a ForEach loop can help you save time.

Complete a replica VM failover

The Complete-VMFailover cmdlet is used as part of the Hyper-V replication process. It allows you to complete the failover of a replica VM. Completing failover for a replica VM deletes all the recovery points on the replica server. You can specify a single VM name or use an asterisk to initiate a "complete failover" process for all replica VMs on the server. For example, to initiate a complete failover process for a single VM on the local replica server, you can use the following command:

Complete-VMFailover –VMName ReplicaVM.

If you need to complete the failover on a remote replica server, you can do so by specifying the replica server name in the –ComputerName parameter:

Complete-VMFailover –VMName ReplicaVM –ComputerName ReplicaServer1.

If you need to initiate a complete failover for more than one replica VM, you can specify multiple VM names separated by commas:

Complete-VMFailover –VMName ReplicaVM1, ReplicaVM2, ReplicaVM3 –ComputerName ReplicaServer1.

Suspending and stopping VM replication: Hyper-V PowerShell offers Suspend-VMReplication and Stop-VMReplication cmdlets to suspend or stop the ongoing replication of VMs. In particular, the Suspend-VMReplication cmdlet comes in handy when you want to suspend ongoing replication for all VMs if you are troubleshooting network-related issues between primary and replica sites. For example, Suspend-VMReplication * suspends replication for all VMs on the local Hyper-V host.

You can also perform the same task from the Hyper-V Manager by right-clicking on each VM, and then selecting "pause replication" from the VM's context menu. Both the Suspend-VMReplication and Stop-VMReplication cmdlets accept multiple VM names and multiple Hyper-V host names, as shown in the following example command:

Suspend-VMReplication –VMName ReplicaVM1, ReplicaVM2, ReplicaVM3 –ComputerName ReplicaServer1.

Granting and revoking VM connect access: In Hyper-V 2012 R2 hosts, you can grant or revoke users' access to certain VMs using the Grant-VMConnectAccess and Revoke-VMConnectAccess cmdlets. For example, if you want to allow User1 to connect to ExchangeVM, you would use the following command:

Grant-VMConnectAccess –VMName ExchangeVM –Username\User1.

If you wanted to revoke users' access to the ExchangeVM, you would instead use:

Revoke-VMConnectAccess –VMName ExchangeVM –Username\User1.

Adding the ForEach loop to your cmdlets

Most Hyper-V PowerShell cmdlets support the –ComputerName parameter, which allows you to execute commands on remote Hyper-V hosts, and the –VMName parameter, which allows you to extend the action to more than one VM. While you can specify multiple VMs and multiple Hyper-V host names, you couldn't use a single command to specify that one VM should be moved to a specific host, while another VM should be moved to a different host. 

Although PowerShell cmdlets can be useful for many management tasks, for more complex or repetitive tasks, you may need to incorporate the ForEach loop. A ForEach loop allows you to execute a PowerShell command multiple times, but for different VMs and Hyper-V hosts. For example, if you wanted to specify the host on which VMs should be failed over, you could create a .CSV file that contains the VM and Hyper-V host names. Your command could then reference this list and process the command via ForEach loop, as shown in the example below:

$VMFile = Import-CSV MyVMs.CSV
ForEach ($Items in $VMFile)
Complete-VMFailover -VMName $VM.VMName –ComputerName $VM.Hyper-VServer                                                                                       }

You can create a .CSV file that contains the VM and Hyper-V host names
You can create a .CSV file that contains the VM and Hyper-V host names

This process can work with other commands, as well. For example, if you want to allow multiple users to access ExchangeVM, use a .TXT file containing the username and the ForEach loop, as shown in the command below:

$AllUsers = Get-Content Users.TXT
ForEach ($ThisUser in $AllUsers)
Grant-VMConnectAccess -VMName ExchangeVM –UserName $ThisUser

Hyper-V PowerShell cmdlets help you increase productivity and automate repetitive tasks, but Hyper-V PowerShell cmdlets alone do not solve every problem. In some cases, you might want to use the ForEach loop to help you better execute cmdlets to specific VMs and Hyper-V hosts.

Next Steps

The top 25 Windows PowerShell commands

Ease Hyper-V virtualization management with PowerShell

What is the best command-line shell?

This was last published in November 2015

Dig Deeper on Microsoft Hyper-V management



Find more PRO+ content and other member only offers, here.

Join the conversation

1 comment

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

How do you use PowerShell ForEach loops?