0% found this document useful (0 votes)
8 views7 pages

Create VM Using PS

Uploaded by

readbook1677
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

Create VM Using PS

Uploaded by

readbook1677
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Create VM on Hyper-V using

PowerShell
This article will show you how to create a Windows 11 virtual machine (VM) on a Hyper-V host using
Windows PowerShell in a Windows Server 2025 environment. The process involves creating the
VM, configuring its settings (like memory, processors, and virtual hard disk), attaching a Windows
11 ISO image, enabling Trusted Platform Module (TPM) for Windows 11 requirements, and starting
the VM for installation. Below, I’ll explain step by step in simple and easy-to-understand way. Let’s
dive in!

Step 1: Introduction
Hyper-V is a virtualization platform in Windows Server that lets you create and run VMs, which are
like virtual computers running on a physical server. Think of it as a magic box that can run multiple
computers inside one physical computer.

Why Use PowerShell? PowerShell allows faster, automated VM creation compared to the GUI (e.g.,
Hyper-V Manager). PowerShell scripts can be reused to create multiple VMs quickly, with
consistent settings. Commands create VMs faster than clicking through GUI menus. Scripts can
deploy many VMs with minimal effort. Ensures all VMs have the same settings.
Key Point: The article focuses on using PowerShell to create and configure a Windows 11 VM
efficiently, ideal for admins deploying multiple VMs.

Step 2: Understanding the Test Lab Setup


Lab Overview:

The lab uses a Windows Server 2025 machine with the Hyper-V role installed.

A Windows 11 ISO image is stored in F:\ISO Image.

The VM will be named Win11-VM02 and stored on the D: drive in a folder called VHDs.

The VM will have:


4 GB of RAM.
100 GB virtual hard disk (VHD).
4 virtual processors.
Generation 2 (required for Windows 11 due to its support for modern features like TPM).

Goal: Create a Windows 11 VM using PowerShell. Configure its settings (processors, ISO, boot
order, TPM). Start the VM and begin the Windows 11 installation.
Key Point: The lab is set up to demonstrate a streamlined VM creation process using PowerShell.

Step 3: Launching PowerShell with Administrative Privileges


Creating and managing VMs requires administrative permissions because Hyper-V interacts with
system resources.
Steps Performed:

1. On the Windows Server 2025 machine, right-click the Start button.

Create VM on Hyper-V using PowerShell 1


2. Select Terminal (Admin) to open PowerShell with administrative privileges.
PowerShell opens in admin mode, allowing commands to manage Hyper-V. Admin privileges are
necessary to execute Hyper-V commands.

Step 4: Creating the Windows 11 VM


The New-VM command quickly sets up a VM with specified settings (name, memory, disk, etc.).
Steps Performed:

1. In PowerShell, type the following command:

New-VM -Name Win11-VM02 -MemoryStartupBytes 4GB -Generation 2 -NewVHDPath


D:\\VHDs\\Win11-VM02.vhdx -NewVHDSizeBytes 100GB

Parameters Explained:

Name Win11-VM02 : Names the VM “Win11-VM02”.

MemoryStartupBytes 4GB : Allocates 4 GB of RAM.

Generation 2 : Creates a Generation 2 VM (required for Windows 11 due to TPM and UEFI
support).

NewVHDPath D:\\VHDs\\Win11-VM02.vhdx : Creates a virtual hard disk (VHDX) at D:\VHDs\Win11-


VM02.vhdx.

NewVHDSizeBytes 100GB : Sets the VHD size to 100 GB.

2. Press Enter to execute the command.

3. The VM is created successfully.

4. Verify the VM exists:

Get-VM

Shows a list of VMs on the Hyper-V host, including Win11-VM02 (in the Off state).

The command creates a VM named Win11-VM02 with 4 GB RAM, a 100 GB VHD, and Generation 2
settings. It’s like building a new virtual computer with specific hardware specs.

Step 5: Setting the Processor Count


VMs need virtual processors to handle tasks. Assigning more processors (e.g., 4) improves
performance.

Steps Performed:

1. Type the following command to set 4 virtual processors:

Create VM on Hyper-V using PowerShell 2


Set-VM -Name Win11-VM02 -ProcessorCount 4

Name Win11-VM02 : Specifies the VM.

ProcessorCount 4 : Assigns 4 virtual CPUs.

2. Press Enter.

3. Verify the processor count:

Get-VM -Name Win11-VM02 | Format-List ProcessorCount

Output shows ProcessorCount : 4 , confirming the setting.

The VM now has 4 virtual processors, like giving a computer 4 CPU cores for better multitasking.

Step 6: Attaching the Windows 11 ISO Image


The Windows 11 ISO image contains the installation files, like a DVD you insert to install an operating
system.

Steps Performed:

1. Type the following command to attach the ISO:

Add-VMDvdDrive -VMName Win11-VM02 -Path F:\\ISO Image\\Windows11.iso

VMName Win11-VM02 : Specifies the VM.

Path F:\\ISO Image\\Windows11.iso : Points to the Windows 11 ISO file.

2. Press Enter.

3. Verify the ISO is attached:

Get-VMDvdDrive -VMName Win11-VM02

Output confirms the ISO is mounted to the VM’s DVD drive.

The VM now has a virtual DVD drive with the Windows 11 ISO, ready for installation.

Step 7: Setting the Boot Order

Create VM on Hyper-V using PowerShell 3


The VM must boot from the DVD drive (ISO) to start the Windows 11 installation, not from the
network or VHD.

Steps Performed:

1. Check the current boot order:

Get-VM -Name Win11-VM02 | Get-VMFirmware | Select BootOrder

Output shows the Network Adapter as the primary boot device.

2. Create a variable for the DVD drive:

$BootImage = Get-VMDvdDrive -VMName Win11-VM02

Stores the DVD drive object in the $BootImage variable.

3. Verify the variable:

$BootImage

Shows the ISO path (e.g., F:\ISO Image\Windows11.iso).

4. Set the DVD drive as the primary boot device:

Set-VMFirmware -VMName Win11-VM02 -FirstBootDevice $BootImage

5. Verify the new boot order:

Get-VM -Name Win11-VM02 | Get-VMFirmware | Select BootOrder

Confirms the DVD Drive is now the primary boot device.

The VM will boot from the Windows 11 ISO first, like setting a computer to boot from a DVD instead
of its hard drive.

Step 8: Enabling Trusted Platform Module (TPM)


Windows 11 requires TPM 2.0 for security features like encryption and secure boot. A virtual TPM
must be enabled for a Generation 2 VM to meet this requirement.

Steps Performed:

1. Attempt to enable TPM:

Create VM on Hyper-V using PowerShell 4


Enable-VMTPM -VMName Win11-VM02

This fails with an error: “The selected security settings of a virtual machine cannot be
changed without a valid key protector configured.”

2. Configure the VM key protector:

Set-VMKeyProtector -VMName Win11-VM02 -NewLocalKeyProtector

Enables a key protector for the VM’s security settings.

3. Re-run the TPM command:

Enable-VMTPM -VMName Win11-VM02

This time, it completes successfully.

The VM now has a virtual TPM, like adding a security chip to a physical computer. The key
protector is like a master key that unlocks TPM settings.

Step 9: Connecting to and Starting the VM


Connecting to the VM lets you see its screen (like plugging in a monitor). Starting the VM begins the
Windows 11 installation from the ISO.

Steps Performed:

1. Connect to the VM:

vmconnect.exe localhost Win11-VM02

localhost : Specifies the local Hyper-V host.

Win11-VM02 : The VM name.

Opens the Virtual Machine Connection window.

2. Start the VM via PowerShell:

Start-VM -Name Win11-VM02

3. In the Virtual Machine Connection window, the VM starts booting from the ISO.

4. Press any key to boot from the ISO and begin the Windows 11 installation.

The VM boots from the Windows 11 ISO, starting the installation process. The vmconnect.exe and Start-

VM commands let you interact with and start the VM.

Create VM on Hyper-V using PowerShell 5


Step 10: Installing Windows 11
Steps Performed (in the Virtual Machine Connection window):

1. The Windows 11 setup starts:

Click Next on the initial screen.

Click Next to select the language and region.

Select Install Windows 11 and check the box to confirm.

Click Next.

Choose Windows 11 LTSC Evaluation and click Next.

Accept the Microsoft license terms and click Next.

Select the unallocated space on the VHD (100 GB) for installation.

Click Next to use the entire space (no partitioning needed).

Click Install to start the installation.

2. The installation begins, and the VM restarts several times (normal for Windows setup).

The VM is now installing Windows 11, like setting up a new physical computer from a DVD.

Step 11: Closing Remarks


Thank You….!

Complete PowerShell Script for Automation (Optional)


Here’s a single PowerShell script that automates the entire process from creating the VM to starting
it. You can save this as a .ps1 file (e.g., Create-Win11VM.ps1 ) and run it in PowerShell (as admin).

# Script to create and configure a Windows 11 VM on Hyper-V


# Run as Administrator

# Variables for VM configuration


$VMName = "Win11-VM02"
$Memory = 4GB
$VHDPath = "D:\\VHDs\\Win11-VM02.vhdx"
$VHDSize = 100GB

Create VM on Hyper-V using PowerShell 6


$ISOPath = "F:\\ISO Image\\Windows11.iso"
$ProcessorCount = 4

# Create the VM
New-VM -Name $VMName -MemoryStartupBytes $Memory -Generation 2 -NewVHDPath $VH
DPath -NewVHDSizeBytes $VHDSize

# Set processor count


Set-VM -Name $VMName -ProcessorCount $ProcessorCount

# Attach the Windows 11 ISO


Add-VMDvdDrive -VMName $VMName -Path $ISOPath

# Set DVD drive as the primary boot device


$BootImage = Get-VMDvdDrive -VMName $VMName
Set-VMFirmware -VMName $VMName -FirstBootDevice $BootImage

# Configure VM key protector and enable TPM


Set-VMKeyProtector -VMName $VMName -NewLocalKeyProtector
Enable-VMTPM -VMName $VMName

# Start the VM
Start-VM -Name $VMName

# Connect to the VM
vmconnect.exe localhost $VMName

Write-Host "Windows 11 VM '$VMName' created and started successfully!"

How to Use the Script:

1. Save it as Create-Win11VM.ps1 .

2. Adjust variables (e.g., $VMName , $ISOPath ) to match your environment.

3. Open PowerShell as Administrator.

4. Run: .\\Create-Win11VM.ps1 .

5. The script creates, configures, and starts the VM, then opens the Virtual Machine Connection
window.

[The End]

Create VM on Hyper-V using PowerShell 7

You might also like