Create a virtual machine by using Windows PowerShell.


Create a virtual machine by using Windows PowerShell

  1. On the Windows desktop, click the Start button and type any part of the name Windows PowerShell.

  2. Right-click Windows PowerShell and select Run as administrator.

  3. 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.

 ~

Create a Virtual Machine with PowerShell

--

  1. Open up the PowerShell ISE as Administrator.

  2. 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,

  1. Start the virtual machine by using the Start-VM cmdlet. Run the following cmdlet where Name is the name of the virtual machine you created.
Start-VM -Name <Name>

For example:

Start-VM -Name autoPIG

Connect to the virtual machine by using Virtual Machine Connection (VMConnect).

VMConnect.exe
  1. https://learn.microsoft.com/en-us/windows-server/virtualization/hyper-v/get-started/create-a-virtual-machine-in-hyper-v

  2. https://learn.microsoft.com/en-us/virtualization/hyper-v-on-windows/quick-start/create-virtual-machine

  3. https://learn.microsoft.com/en-us/powershell/module/hyper-v/set-vmmemory?view=windowsserver2022-ps

  4. https://www.thomasmaurer.ch/2020/07/how-to-manage-hyper-v-vm-checkpoints-with-powershell/