Auto-Run Scripts in Windows 11 Sandbox
Running automated testing scripts inside Windows Sandbox allows you
to safely test software, scripts, and configurations in an isolated,
disposable environment. This article provides a step-by-step guide on
creating custom Windows Sandbox Configuration (.wsb) files
to map host directories and automatically execute Batch or PowerShell
scripts upon launch in Windows 11.
Step 1: Prepare Your Testing Script
Create the script you want to run inside the sandbox. For this
example, create a folder on your host machine such as
C:\SandboxScripts and place your script inside it.
If using a PowerShell script (test.ps1), create a
wrapper Batch file (run.cmd) to bypass execution policies
cleanly:
powershell.exe -ExecutionPolicy Bypass -File "C:\Scripts\test.ps1"Step 2: Create the
.wsb Configuration File
Windows Sandbox uses XML-based configuration files with the
.wsb extension.
- Open Notepad or your preferred text editor.
- Paste the following XML structure:
<Configuration>
<VGpu>Default</VGpu>
<Networking>Default</Networking>
<MappedFolders>
<MappedFolder>
<HostFolder>C:\SandboxScripts</HostFolder>
<SandboxFolder>C:\Scripts</SandboxFolder>
<ReadOnly>true</ReadOnly>
</MappedFolder>
</MappedFolders>
<LogonCommand>
<Command>C:\Scripts\run.cmd</Command>
</LogonCommand>
</Configuration>Configuration Elements Explained:
<MappedFolder>: Maps a folder from the host OS (HostFolder) to a virtual path inside the sandbox (SandboxFolder).<ReadOnly>: Set totrueto prevent the sandbox environment from modifying files on your host machine.<LogonCommand>: Specifies the exact command or executable that runs automatically once the sandbox completes its boot process.
Step 3: Save and Run the Configuration
- In Notepad, select File > Save As.
- Set Save as type to All Files (.).
- Name the file with a
.wsbextension (for example,AutoTest.wsb). - Double-click the
AutoTest.wsbfile.
Windows Sandbox will launch, mount your host script directory, and automatically trigger your testing script via the logon command. When testing is complete, closing Windows Sandbox completely discards all changes and temporary data.