Automating Windows 11 Hyper-V Deployments with PowerShell

Building and maintaining a virtual lab can be time consuming, especially when each virtual machine needs manual configuration after first boot. If you work with Microsoft Hyper-V, Windows 11 images, or VDI environments, automation can save a significant amount of time and deliver a more consistent setup. In this post, I walk through the process demonstrated in the video that shows how to update a Windows 11 image so it bypasses the out of box experience, auto renames itself based on the Hyper-V VM name, and then uses PowerShell to automatically deploy multiple VMs from that customized image.

All scripts referenced in this blog post are linked below.

Preparing the Windows 11 Image

In the previous video, I created a Windows 11 image that skips the out of box experience and boots directly to the login screen. This is helpful for automated labs because it avoids the usual setup prompts. However, one problem remained. Windows automatically generates a random computer name during setup and that name does not match the VM name defined in Hyper-V. When managing many VMs, this mismatch causes confusion, especially when connecting remotely. To fix that, we need the operating system to rename itself to match the Hyper-V VM name on first boot.

To make that possible, I added a PowerShell script that runs during the setupcomplete phase. A batch file placed in the C:\Windows\Setup\Scripts directory calls the PowerShell script.

During startup, Windows detects the batch file and executes it after the out of box experience completes. The script reads the Hyper-V guest name from a registry value and compares it to the current OS name. If the names do not match, the script renames the computer and triggers a reboot. Logging is built in so you can track successes and errors during the process.

Once the scripts are ready, the image template VHD is mounted, and the files are copied into the C:\Windows\Setup\Scripts folder. When new virtual machines are created from this image, the automated rename happens on their very first boot without any manual involvement.

Automating Hyper-V VM Creation with PowerShell

With the Windows image prepared, the next step is to automate the creation of multiple Hyper-V virtual machines. This helps build consistent test environments, VDI setups, and homelabs quickly. I created a PowerShell script that generates a predefined number of VMs based on a name prefix. The prefix becomes part of each VM name as well as the operating system name after the rename script runs.

The PowerShell deployment script accepts required parameters such as the name prefix, the number of VMs to create, the path to the source image, and the destination path for the new VMs. Optional parameters allow you to set memory, CPU counts, and whether the machines should start automatically once created. The script verifies paths, constructs sequential VM names, checks for conflicts, and loops through the list to generate each VM and attach the VHDX file. After creation, the VMs start unless this behavior is disabled. The rename script inside the image then kicks in to align each OS name to each Hyper-V VM name.

Final Thoughts

Automating the Windows 11 image process along with Hyper-V VM deployment reduces configuration time and removes many manual steps from the workflow. The combination of SetupComplete scripting and PowerShell VM provisioning creates a repeatable and reliable way to produce fully prepared virtual machines. For anyone managing test labs or working in VDI environments, this approach can significantly streamline daily operations.

The accompanying video includes a full walkthrough of the scripts. If you want to explore the scripts or adapt them for your own environment, links are included below.

Links:

Zero to Hero with Azure Virtual Desktop
https://www.udemy.com/course/zero-to-hero-with-windows-virtual-desktop/?referralCode=B2FE49E6FCEE7A7EA8D4

A Beginner’s Guide to the AZ-900
https://www.udemy.com/course/beginners-guide-az-900/?referralCode=C74C266B74E837F86969

Hybrid Identity with Windows AD and Azure AD
https://www.udemy.com/course/hybrid-identity-and-azure-active-directory/?referralCode=7F62C4C6FD05C73ACCC3

Windows 365 Enterprise and Intune Management
https://www.udemy.com/course/windows-365-enterprise-and-intune-management/?referralCode=4A1ED105341D0AA20D2E

Scripts Used in the Video
https://github.com/tsrob50/CiraltosTools

Creating a Windows 11 Image That Bypasses OOBE in Hyper-V

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Click Here!
Scroll to Top