Windows Sandbox provides a safe, disposable environment for executing untrusted software and testing scripts. While the standard <MappedFolders> directive enables host directory sharing, certain advanced workflows—such as inspecting partition tables, testing file system utilities, or mounting isolated disk images—require genuine block-level virtual hard drives. By combining .wsb XML automation with PowerShell logon scripts, you can mount .vhd and .vhdx files inside Windows Sandbox automatically upon boot.
Understanding Sandbox Storage and Folder Mapping Directives
The native Windows Sandbox XML configuration schema directly supports folder redirection rather than raw virtual disk attachments. However, by mapping a host folder containing your target .vhdx file and executing a privileged logon script, Windows Sandbox can attach the virtual disk using native Windows disk management services.
Authoring the Automated Disk Mounting Script
Create a dedicated host folder (for example, C:\SandboxDiskShare) and place your pre-configured TestDisk.vhdx inside it. In the same folder, save a PowerShell automation script named MountVhd.ps1:
# MountVhd.ps1 - Automated disk attachment script
$vhdPath = "C:DiskShareTestDisk.vhdx"
Write-Host "Waiting for Virtual Disk Service to initialise..." -ForegroundColor Cyan
Start-Sleep -Seconds 3
if (Test-Path $vhdPath) {
Write-Host "Mounting VHDX: $vhdPath" -ForegroundColor Green
Mount-DiskImage -ImagePath $vhdPath -StorageType VHDX
# Assign drive letter if not automatically allocated
$diskImage = Get-DiskImage -ImagePath $vhdPath
$partition = Get-Disk -Number $diskImage.Number | Get-Partition | Where-Object { $_.Type -eq 'Basic' }
if ($partition -and -not $partition.DriveLetter) {
Set-Partition -DiskNumber $diskImage.Number -PartitionNumber $partition.PartitionNumber -NewDriveLetter "V"
Write-Host "Assigned Virtual Drive Letter: V:" -ForegroundColor Green
}
} else {
Write-Warning "Target VHDX not found at $vhdPath"
}
Constructing the WSB Configuration File
Create an XML configuration file named VhdSandbox.wsb on your desktop:
<Configuration>
<VGpu>Enable</VGpu>
<Networking>Default</Networking>
<MemoryInMB>8192</MemoryInMB>
<MappedFolders>
<MappedFolder>
<HostFolder>C:SandboxDiskShare</HostFolder>
<SandboxFolder>C:DiskShare</SandboxFolder>
<ReadOnly>false</ReadOnly>
</MappedFolder>
</MappedFolders>
<LogonCommand>
<Command>powershell.exe -NoExit -ExecutionPolicy Bypass -File C:DiskShareMountVhd.ps1</Command>
</LogonCommand>
</Configuration>
Launching and Verifying the Mounted Virtual Volume
To run the automated sandbox session:
- Double-click the
VhdSandbox.wsbconfiguration file in Windows 11. - Windows Sandbox launches and opens an elevated PowerShell window executing
MountVhd.ps1. - Open Windows File Explorer inside the Sandbox and confirm that virtual drive
V:appears under This PC. - Perform your disk operations, application installations, or file tests within the mounted virtual volume.
Ensuring Host Storage Integrity
If you set <ReadOnly>true</ReadOnly> in the mapped folder configuration, Mount-DiskImage attaches the VHDX in read-only mode, guaranteeing that any malware or erratic test software inside the sandbox cannot alter the underlying virtual disk file on your host system.