Create a virtual machine by using Windows PowerShell
On the Windows desktop, click the Start button and type any part of the name Windows PowerShell.
Right-click Windows PowerShell and select Run as administrator.
Get the name of the virtual switch that you want the virtual machine to use by using Get-VMSwitch. For example,
Get-VMSwitch * | Format-Table Name
Use the New-VM cmdlet to create the virtual machine. See the following examples.
--
Open up the PowerShell ISE as Administrator.
Run the following script.
EXAMPLE 0. New virtual hard disk that boots to operating system image (Generation 1)
# Set VM Name, Switch Name, and Installation Media Path.
$VMName = 'autoPIG'
$Switch = 'Default Switch'
$InstallMedia = 'F:\ISO\debian-115-sudo-unattended-M9o.iso'
# Create New Virtual Machine
New-VM `
-Name $VMName `
-MemoryStartupBytes 2GB `
-Generation 1 `
-SwitchName $Switch
# Add DVD Drive to Virtual Machine
Add-VMScsiController -VMName $VMName
Add-VMDvdDrive `
-VMName $VMName `
-ControllerNumber 1 `
-ControllerLocation 0 `
-Path $InstallMedia
# Mount Installation Media
$DVDDrive = Get-VMDvdDrive -VMName $VMName
# Configure Virtual Machine to Boot from DVD
Set-VMBios `
-VMName autoPIG `
-StartupOrder `
@("CD", "IDE", "LegacyNetworkAdapter", "Floppy")
EXAMPLE 1. New virtual hard disk that boots to operating system image (GENERATION-2)
### RENAME this file to newVM.ps1 to make this
### script recognizable by Powershell.
### Apply necessary policy to allow local scripts:
### Get-ExecutionPolicy,
### Set-ExecutionPolicy { remotesigned | unrestricted }
# Set VM Name, Switch Name, and Installation Media Path.
$VMName = 'autoPIG'
$Switch = 'Default Switch'
$InstallMedia = 'F:\ISO\debiuefi1.iso'
# Create New Virtual Machine
Remove-VM $VMName -Force
rm "C:\Users\Public\Documents\Hyper-V\Virtual hard disks\$VMName.vhdx"
# -Path "D:\VM\$VMName" `
New-VM `
-Name $VMName `
-MemoryStartupBytes 2GB `
-Generation 2 `
-NewVHDPath "C:\Users\Public\Documents\Hyper-V\Virtual hard disks\$VMName.vhdx" `
-NewVHDSizeBytes 32GB `
-SwitchName $Switch
Set-VMMemory `
-VMName $VMName `
-DynamicMemoryEnabled $False
Set-VM -Name $VMName -CheckpointType Disabled
Set-VMFirmware -VMName $VMName -EnableSecureBoot off
# Add DVD Drive to Virtual Machine
Add-VMScsiController -VMName $VMName
Add-VMDvdDrive `
-VMName $VMName `
-ControllerNumber 1 `
-ControllerLocation 0 `
-Path $InstallMedia
# Mount Installation Media
$DVDDrive = Get-VMDvdDrive -VMName $VMName
# Configure Virtual Machine to Boot from DVD
Set-VMFirmware `
-VMName $VMName `
-FirstBootDevice $DVDDrive
EXAMPLE 2. New virtual hard disk
New-VM `
-Name autoPIG `
-MemoryStartupBytes 4GB `
-BootDevice VHD `
-NewVHDPath .\VMs\Win10.vhdx `
-Path .\VMData `
-NewVHDSizeBytes 20GB `
-Generation 2 `
-Switch ExternalSwitch
EXAMPLE 3. Existing virtual hard disk
Existing virtual hard disk -- To create a virtual machine with an existing virtual hard disk, you can use the following command
New-VM -Name <Name> -MemoryStartupBytes <Memory> -BootDevice <BootDevice> -VHDPath <VHDPath> -Path <Path> -Generation <Generation> -Switch <SwitchName>
For example:
New-VM `
-Name autoPIG `
-MemoryStartupBytes 4GB `
-BootDevice VHD `
-VHDPath .\VMs\Win10.vhdx `
-Path .\VMData `
-Generation 2 `
-Switch ExternalSwitch
where,
-Name is the name that you provide for the virtual machine that you're creating.
-MemoryStartupBytes is the amount of memory that is available to the virtual machine at start up.
-BootDevice is the device that the virtual machine boots to when it starts like the network adapter (NetworkAdapter) or virtual hard disk (VHD).
-VHDPath is the path to the virtual machine disk that you want to use.
-Path is the path to store the virtual machine configuration files.
-Generation is the virtual machine generation. Use generation 1 for VHD and generation 2 for VHDX. See Should I create a generation 1 or 2 virtual machine in Hyper-V?.
-Switch is the name of the virtual switch that you want the virtual machine to use to connect to other virtual machines or the network. See Create a virtual switch for Hyper-V virtual machines.
Start-VM -Name <Name>
For example:
Start-VM -Name autoPIG
Connect to the virtual machine by using Virtual Machine Connection (VMConnect).
VMConnect.exe