Windows Server 2016 introduced full PowerShell support with over 2,300 commandlets, including the Hyper-V module, which comes with 232 commands. It is true that the Graphical User Interface (GUI) is easier than almost all the other options. But PowerShell Version 5 proved that it can be handy in most common scenarios.
This recipe will demonstrate how easy it is to use PowerShell and provide you with some helpful information, with examples, to guide you in using the commands with no advanced knowledge or a lot of effort.
To get ready, you just need to open a PowerShell window as administrator, from the taskbar, and run the command Update-Help
to update all the existing help content. To update the Help command, you will need an Internet connection.
The following steps will walk you identifying the existing Hyper-V commands and how to get more information about how to use them:
Get-Command
commandlet, specifying the module that you want to see, as shown in the following example. A list of all the 232 commandlets for Hyper-V will show up, as shown in the following screenshot:Get-Command –Module Hyper-V
Get-Command
with the Name switch. Using the following example as a reference, you can change the term between the asterisks at the end of the command to find the word that you are looking for. In the example, PowerShell shows every command from the Hyper-V module that contains the word vhd
:Get-Command –Module Hyper-V –Name *vhd*
noun
or verb
, as shown in the following examples, to find commands with the noun VM and the verb Start:Get-Command -noun VM Get-Command –Verb Start
Help
command. By using the Get-Help
command, you can understand what you can do with the commandlet, and how to do it. For example, if you want to see what you can do with the New-VM
command, you can type the following Help commands, the last one being handy for showing examples of the usages of New-VM
:Get-Help New-VM Get-Help New-VM –Detailed Get-Help New-VM –Full Get-Help New-VM –Examples
New-VM
, you can use the following command to create a virtual machine named NewVM
, with 512MB
, of memory at the path C:Hyper-VVMs
:New-VM –Name NewVM –MemoryStartupBytes 512MB –path C:Hyper-VVMs
NewVM
with 512MB
of memory:New-VM –Name NewVM –MemoryStartupBytes 512MB –path C:Hyper-VVMs New-VM –Name NewVM New-VM
Show-Command
.PowerShell offers different commands, each one with lots of switches and options that are difficult to remember. The purpose of this recipe is to show how simple it can be knowing more about the commands in PowerShell by using some commands. We used Get-Command
to find other commands, the helpful Get-Help
command using the Examples switch to show some nice examples, and the GUI provided by Show-Command
to help IT admins who don't like the command-line interface.
After working through these steps, you can identify and learn how to use the Hyper-V commands on PowerShell.
Keeping the previous example in mind, you can create some new virtual machines with the New-VM
command, and then you can explore some other advanced options using loops and variables to create lots of VMs in just one command line. In the following command, there is a loop from one to ten, which will create ten virtual machines, with the name starting with NewVM
followed by the loop number:
1..10 | % { New-VM -Name "NewVM$_" }
The following screenshot shows the 10 virtual machines that were created after running the command line:
3.17.6.75