Hyper-V: Creating Virtual Machines With PowerShell

As I wrote in my previous article, managing Hyper-V from PowerShell is quite easy. You can:

  • create,
  • configure,
  • and manage your virtual machines with few command lines.

First thing, you need to install the Hyper-V PowerShell module.

Now, you can run the New-VM PowerShell cmdlet as below:

PS C:\> New-VM -Name VMTestNicolas `
       -Path "C:\ProgramData\Microsoft\Windows\Hyper-V\Virtual Machines" `
       -NewVHDPath "C:\ProgramData\Microsoft\Windows\Hyper-V\Virtual Hard Disks\VMTestNicolas.VHDX" `
       -NewVHDSizeBytes 25GB `
       -Generation 2 `
       -MemoryStartupBytes 1GB `
       -SwitchName LAN


Name          State CPUUsage(%) MemoryAssigned(M) Uptime   Status             Version
----          ----- ----------- ----------------- ------   ------             -------
VMTestNicolas Off   0           0                 00:00:00 Operating normally 7.0

Let’s check if the VM is created with GUI:

2016-07-09 16_47_02-Hyper-V Manager

or with PowerShell:

PS C:\> get-vm

Name          State   CPUUsage(%) MemoryAssigned(M) Uptime             Status             Version
----          -----   ----------- ----------------- ------             ------             -------
VMTestNicolas Off     0           0                 00:00:00           Operating normally 7.0
Windows 7     Off     0           0                 00:00:00           Operating normally 7.0
Windows 8.1   Running 0           590               7.13:11:45.8200000 Operating normally 7.0
Windows10     Off     0           0                 00:00:00           Operating normally 7.0

Now, we will check the network settings (SwitchName is LAN):

PS C:\> get-vm -Name VMTestNicolas | Get-VMNetworkAdapter

Name            IsManagementOs VMName        SwitchName MacAddress   Status IPAddresses
----            -------------- ------        ---------- ----------   ------ -----------
Network Adapter False          VMTestNicolas LAN        000000000000        {}

Check the VHDX path (C:\ProgramData\Microsoft\Windows\Hyper-V\Virtual Hard Disks\VMTestNicolas.VHDX):

PS C:\> get-vm -Name VMTestNicolas | Get-VMHardDiskDrive

VMName        ControllerType ControllerNumber ControllerLocation DiskNumber Path
------        -------------- ---------------- ------------------ ---------- ----
VMTestNicolas SCSI           0                0                             C:\ProgramData\Microsoft\Windows\Hyper-V\Virtual Hard Disks\VMTestNicolas.VHDX

Check the Startup Memory (1GB):

PS C:\> get-vm -Name VMTestNicolas | Get-VMMemory

VMName        DynamicMemoryEnabled Minimum(M) Startup(M) Maximum(M)
------        -------------------- ---------- ---------- ----------
VMTestNicolas True                 512        1024       1048576

Check the VM Generation (VM Gen 2):

PS C:\> get-vm -Name VMTestNicolas | select VirtualMachineSubType

VirtualMachineSubType
---------------------
          Generation2

 

 

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.