- SystoLOCK in review: Logging in to Active Directory with multi-factor authentication without passwords - Tue, Dec 5 2023
- New Group Policy settings in Windows 11 23H2 - Mon, Nov 20 2023
- Windows Server 2025 will support SMB over QUIC in all editions - Fri, Nov 17 2023
DISM and the cmdlets of the namesake PowerShell module are not only used for processing WIM archives but have also been extended for servicing VHDs for some time. This capability can be used to automate the configuration of the guest operating system.
This is especially true if you previously installed Windows on a VHD without user interaction using Wim2VHD. Then, instead of attaching the virtual drive to a VM and booting it up, you can add the features you need right away offline.
The steps required for this, namely mounting the VHD, activating the features, unmounting the image, and writing the changes, can easily be packed into a script and processed in one go.
If you decide to use DISM, mount the VHD according to the following pattern:
dism /mount-image /imageFile:"F:\myvm.vhdx" /MountDir:F:\mnt /index:1
The parameter index must always have a value of 1 for VHD(X)s. The equivalent command in PowerShell looks like this:
Mount-WindowsImage -Path f:\mnt -ImagePath "F:\myvm.vhdx" -Index 1
Displaying and activating features
Next, you can display the list of Windows features to determine their exact names for activation.
Dism /Image:F:\mnt /Get-Features
If you want to display only a specific option from the long list of features, pipe the output to findstr.exe.
With PowerShell, on the other hand, you can specify the name right away. Wildcards are also allowed:
Get-WindowsOptionalFeature -Path f:\mnt -FeatureName Hyper*
Then, activate the required feature in this way:
Dism /Image:F:\mnt /enable-Feature:Microsoft-Hyper-V /ALL
This command would install Hyper-V.
The PowerShell version looks like this:
Enable-WindowsOptionalFeature -Path f:\mnt -FeatureName Microsoft-Hyper-V
This method of installing Hyper-V using nested virtualization can prove useful for a lab running Azure Stack HCI. If you want to activate the hypervisor in the online host VM, it might fail due to the prerequisite check, even though the prerequisites are objectively met. However, if you add it offline, there is a good chance that Hyper-V will work in the VM afterward.
Unmount VHD and save changes
Finally, you need only unmount the VHDX and save the changes. To do so, do one of the following:
dism /unmount-image /mountDir:F:\mnt /commit
or in PowerShell:
Subscribe to 4sysops newsletter!
Dismount-WindowsImage -Path f:\mnt -Save
The virtual drive can now be added to a VM if it has not already been added.