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

Pick and choose when to use Hyper-V PowerShell cmdlets

The amount of time you can save by using PowerShell cmdlets is enticing but in certain situations you should explore other methods.

Microsoft has put in a lot of effort into designing a scripting framework that helps IT administrators get information...

from roles and features installed on Windows Server OSes. Almost every role or feature ships with a set of PowerShell cmdlets that you can use to reduce the time it takes to perform a task. The Hyper-V role also ships with PowerShell cmdlets that you can use to interact with Hyper-V host and virtual machines running on them. For example, you can use the command Get-VM to list VMs running on a particular Hyper-V host or the commands Start-VM and Stop-VM to start or stop a single or multiple VMs.

Not all Hyper-V PowerShell cmdlets are useful

Although it is certainly true that performing a task with a command takes less time than doing the same task from a GUI, using a command is not for every task. For example, in the case of Hyper-V, before you can add an external virtual switch using the New-VMSwitch PowerShell cmdlet, you will need to know the available physical network adapter to which the new external virtual switch will be mapped to. From the PowerShell window, you can use Get-NetAdapter to get all physical network adapters. Next, you need to specify the switch type using the –SwitchType parameter, setting any required options such as –AllowManagementOS and then typing the command to create a new external virtual switch on a Hyper-V host. Similarly, in case you need to connect one or more VM virtual network adapters to a Hyper-V virtual switch, you can use Connect-VMNetworkAdapter.

Although you have PowerShell cmdlets available to create an external virtual switch and connect virtual network adapters to a Hyper-V virtual switch, how many times in a day do you perform these tasks? Creating external virtual switches on Hyper-V hosts are part of a planning and designing process and you will find it easier to create a Hyper-V virtual switch using the Hyper-V Manager rather than using a PowerShell cmdlet. Generally, you don't remove and create virtual switches every day. Using Connect-VMNetworkAdapter can be useful if you want to connect virtual network adapters of multiple VMs to a Hyper-V virtual switch. However, you can specify the name of the VMs separated by a comma and the Hyper-V virtual switch name in the –SwitchName parameter to connect the virtual network adapter of multiple VMs to a Hyper-V virtual switch. It is important to note that in a production environment, you don't connect/disconnect VMs to a Hyper-V virtual switch every day.

Why you should use Hyper-V PowerShell cmdlets

Although they might not be for every scenario, there are several reasons why you should consider using Hyper-V PowerShell cmdlets.

In case you are designing a PowerShell script that performs repeated tasks, you can use Hyper-V cmdlets in a script. Most of the Hyper-V PowerShell cmdlets ship with "-WhatIf" parameter. This parameter, once specified with a cmdlet, shows what would happen if the cmdlet runs. Remember that this option is not available with Hyper-V Manager. If you use Hyper-V Manager to perform a task, the task is executed immediately. Although Hyper-V Manager GUI will seek your confirmation, the results are known only after the task has been executed.

Most of the Hyper-V PowerShell cmdlets accept multiple inputs such as specifying multiple VMs or Hyper-V hosts in a cmdlet, which is very useful if you want to execute an urgent task on more than one VM or more than one Hyper-V host without spending much time working with Hyper-V Manager. For example, in case there is a maintenance activity on one of the Hyper-V hosts and you need to save all VMs or live migrate all VMs to one of the Hyper-V hosts, PowerShell cmdlets Suspend-VM and Move-VM play a vital role. To suspend all VMs running on local Hyper-V hosts, you can use Get-VM | Where State –eq "running" | Suspend-VM command. In case you need to suspend all VMs on a remote Hyper-V host, you can use the suspend or invoke command as it appears below Invoke-Command {Get-VM | Where State –eq "Running" | Suspend-VM} –ComputerName Hyper-VHost1.

Finally, most of the new features of Hyper-V 2012 R2 can only be configured using Hyper-V PowerShell cmdlets, including enabling resource metering for the VMs, enabling port mirroring, setting up some replication options such as –BypassProxy server setting or enabling resynchronization of the VMs, and granting/revoking single or multiple users access to connect to VMs running on Windows Server 2012 R2 and later Hyper-V hosts. You have no choice except to use PowerShell cmdlets for these tasks. For example, Windows Server 2012 R2 and later Hyper-V hosts enable controlling user access to connect to VMs, but you can only control access to VMs by using the Grant-VMConnectAccess and Revoke-VMConnectAccess cmdlets. Hyper-V Manager does not provide any option to grant/revoke users access to connect to VMs. As an example, to allow user1 to connect to a SQLVM running on a 2012 R2 Hyper-V host, you will use Grant-VMConnectAccess –VMName SQLVM –Username Domain.com\User1 and to revoke access, you will use the Revoke-VMConnectAccess –VMName SQLVM –Username Domain.com\User1 command.

PowerShell has been an integral part of Microsoft's server and client computing. It is certainly true that Hyper-V PowerShell cmdlets help you increase the productivity and also help in automating repeated tasks, but not all Hyper-V cmdlets are as helpful as explained above. Hyper-V Manager GUI can be used to quickly execute a task, but not all Hyper-V tasks can be performed using Hyper-V Manager.

Next Steps

Top Hyper-V PowerShell cmdlets for basic tasks

The Windows PowerShell cmdlets you need to know

Overcome Hyper-V live migration limitations with PowerShell cmdlets

This was last published in September 2015

Dig Deeper on Microsoft Hyper-V and Virtual Server

PRO+

Content

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.

When do you think Hyper-V PowerShell cmdlets help the most?
Cancel

-ADS BY GOOGLE

SearchVMware

SearchWindowsServer

SearchCloudComputing

SearchVirtualDesktop

SearchDataCenter

Close