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.

  1. Open Notepad or your preferred text editor.
  2. 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:


Step 3: Save and Run the Configuration

  1. In Notepad, select File > Save As.
  2. Set Save as type to All Files (.).
  3. Name the file with a .wsb extension (for example, AutoTest.wsb).
  4. Double-click the AutoTest.wsb file.

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.