Managing Hyper-V with PowerShell

Hyper-V can be managed using the GUI or … using PowerShell! Before starting, just make sure that Hyper-V role is installed with the following command:

PS> Get-WindowsFeature -Name Hyper-V

Display Name                                            Name                       Install State
------------                                            ----                       -------------
[X] Hyper-V                                             Hyper-V                        Installed

If Hyper-V role is not installed, run the “Install-WindowsFeature” cmdlet:

Install-WindowsFeature -Name Hyper-V -IncludeManagementTools -Restart

Do not forget to include the “-IncludeManagementTools” parameter because it will install the PowerShell module for Hyper-V.

Be careful, this command will force a reboot.

Now you can easily get the list of Hyper-V cmdlets by using the following command:

Get-Command –Module "Hyper-V"

Output will be something like that:

hyperv

PowerShell let you accomplish most of the tasks that you can accomplish with the GUI.

Listing Hyper-V Hosts and Virtual Machines

Let’s see two examples. first, I will list my Hyper-V host and then I will list its VMs.

1- If you want to display the local Hyper-V host:

PS> Get-VMHost

Name   LogicalProcessorCount MemoryCapacity(M) VirtualMachineMigrationEnabled
----   --------------------- ----------------- ------------------------------
HYPV01 24                    122869,796875     False

2- If you want to display all the VMs:

PS> Get-VM

Name                    State   CPUUsage(%) MemoryAssigned(M) Uptime     Status
----                    -----   ----------- ----------------- ------     ------
DeployIIS1              Running 0           4096              5.17:23:06 Operating normally
IISSysprep              Saved   0           0                 00:00:00   Operating normally
TemplateSQL             Saved   0           0                 00:00:00   Operating normally
testDB1                 Saved   0           0                 00:00:00   Operating normally
TestIIS01               Running 0           4096              5.16:22:33 Operating normally
testIIS4                Running 0           4096              5.17:23:19 Operating normally
testIIS5                Running 0           4096              5.17:22:29 Operating normally
testsql02               Running 0           4096              5.17:22:57 Operating normally
testsql03               Off     0           0                 00:00:00   Operating normally
testsql04               Running 0           4096              5.17:22:32 Operating normally
testsql07               Running 0           4096              5.17:22:28 Operating normally

Many cmdlets let you display some information about your Hyper-V environment. You can list these cmdlets with the following command:

Get-Command Get-VM*

hyperv2

If you need further information about these cmdlets, you can use the “Get-Help” cmdlet.

 

Thanks for reading! You can follow me on Twitter @PrigentNico

 

About Nicolas 282 Articles
I work as an IT Production Manager, based in Paris (France) with a primary focus on Microsoft technologies. I have 10 years experience in administering Windows Servers. . I am a Microsoft MVP for Cloud & Datacenter Management. I also received the PowerShell Hero 2016 award by PowerShell.0rg. And finally, I am "MCSE: Cloud Platform and Infrastructure", "MCSA: Windows Servers", "Administering & Deploying SCCM", and CheckPoint CCSA certified.