Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

How to Start Windows Sandbox with Parameters Using PowerShell

May 29, 2024
in Blog, Powershell, Windows Sandbox
0
ADVERTISEMENT

Table of Contents

I frequently utilize Windows Sandbox to test Endpoint Manager packages and software. Occasionally, I need to launch it with particular options, such as connecting a folder from my hard drive or initiating it without a network connection. Typically, this requires creating a custom configuration file (.wsb) with the desired settings. However, this blog post demonstrates how to initiate Windows Sandbox using PowerShell with parameters, eliminating the need for multiple configuration files.

What is Windows Sandbox?

Windows Sandbox offers a lightweight desktop environment for safely running applications in isolation. Software installed in the Windows Sandbox environment stays ‘sandboxed’ and operates independently from the host machine. The sandbox is temporary, once closed, all software, files, and state are deleted.

You can install it by adding it as a Windows Feature using Add/Remove programs or by running:

Enable-WindowsOptionalFeature -Online -FeatureName:Containers-DisposableClientVM -NoRestart:$True

How the script works

I utilized all the options detailed at the official Microsoft documentation to develop a Start-Sandbox function that generates a .wsb file, which is essentially an XML file. It creates a basic file if no parameters are specified, but it includes additional lines if more parameters are provided.

Once the .wsb file is created, the script initiates Windows Sandbox with this file and subsequently removes the configuration file from the temporary directory.

Below are all the parameters that you can use in the Start-Sandbox function:

  • vGPUdisable: Use this to disable vGPU sharing, making the Windows Sandbox use software rendering.
  • AudioInputDisable: Use this to disable the microphone access in Windows Sandbox.
  • ClipboardRedirectionDisable: Use this to completely disable the copy/paste function between your computer and Windows Sandbox (In and outgoing).
  • LogonCommand: Specify the path to the executable or script that should be started when the Windows Sandbox is running.
  • MappedFolder: Use this to specify a local folder you want to see in your Windows Sandbox session, for example, D:\temp.
  • MappedFolderWriteAccess: Use this to switch from Read-Only mode to Read-Write mode for the MappedFolder you specified.
  • MemoryInMB: Use this to specify the amount of RAM in MBS that Windows Sandbox should use. Specifying something below 2Gb will show a warning telling you that Windows Sandbox could allocate more memory if needed.
  • NetworkingDisable: Use this to disable networking in Windows Sandbox, it could be a good thing when testing software that you’re not entirely sure about 😉
  • PrinterRedirectionEnable: Use this to connect your local printers in Windows Sandbox.
  • ProtectedClientEnable: Use this so that Windows Sandbox will run with extra security mitigations enabled.
  • VideoInputEnable: Use this to enable video input in Windows Sandbox.

Below is the script for the Start-Sandbox function:

function Start-Sandbox {
    param(
        [parameter(Mandatory = $false)][string]$MappedFolder,
        [parameter(Mandatory = $false)][string]$MemoryInMB,
        [parameter(Mandatory = $false)][string]$LogonCommand,
        [switch]$vGPUdisable,
        [switch]$AudioInputDisable,
        [switch]$ClipboardRedirectionDisable,
        [switch]$MappedFolderWriteAccess,
        [switch]$NetworkingDisable,
        [switch]$PrinterRedirectionEnable,
        [switch]$ProtectedClientEnable,
        [switch]$VideoInputEnable
    )

    #Validate if $mappedfolder exists
    if ($MappedFolder) {
        if (Test-Path $MappedFolder -ErrorAction SilentlyContinue) {
            Write-Host ("Specified {0} path exists, continuing..." -f $MappedFolder) -ForegroundColor Green
        }
        else {
            Write-Host ("Specified {0} path doesn't exist, exiting..." -f $MappedFolder) -ForegroundColor Red
            return
        }
    }
    #Set Read-Only or Read-Write
    if ($MappedFolderWriteAccess) {
        $WriteAccess = 'false'
    }
    else {
        $WriteAccess = 'true'
    }
    #Create .wsb config file
    $wsb = @()
    $wsblocation = "$($env:Temp)\sandbox.wsb"
    $wsb += "<Configuration>"
    if ($vGPUdisable) {
        $wsb += "<VGpu>Disable</VGpu>"
    }

    if ($AudioInputDisable) {
        $wsb += "<AudioInput>Disable</AudioInput>"
    }

    if ($ClipboardRedirectionDisable) {
        $wsb += "<ClipboardRedirection>Disable</ClipboardRedirection>"
    }

    if ($MappedFolder) {
        $wsb += "<MappedFolders>"
        $wsb += "<MappedFolder>"
        $wsb += "<HostFolder>$($MappedFolder)</HostFolder>"
        $wsb += "<ReadOnly>$($WriteAccess)</ReadOnly>"
        $wsb += "</MappedFolder>"
        $wsb += "</MappedFolders>"
    }

    if ($null -ne $MemoryInMB) {
        $wsb += "<MemoryInMB>$($MemoryInMB)</MemoryInMB>"
        if ($MemoryInMB -le 2048) {
            Write-Host "$($MemoryInMB) Mb(s) specified, Windows Sandbox will automatically allocate more if needed..." -ForegroundColor Yellow
        }
    }

    if ($NetworkingDisable) {
        $wsb += "<Networking>Disable</Networking>"
    }

    if ($LogonCommand) {
        $wsb += "<LogonCommand>"
        $wsb += "<Command>$($LogonCommand)</Command>"
        $wsb += "</LogonCommand>"
    }

    if ($PrinterRedirectionEnable) {
        $wsb += "<PrinterRedirection>Enable</PrinterRedirection>"
    }

    if ($ProtectedClientEnable) {
        $wsb += "<ProtectedClient>Enable</ProtectedClient>"
    }

    if ($VideoInputEnable) {
        $wsb += "<VideoInput>Enable</VideoInput>"
    }

    $wsb += "</Configuration>"
    
    #Create sandbox .wsb file in $env:\temp and start Windows Sandbox using it
    $wsb | Out-File $wsblocation -Force:$true
    Write-Host ("Starting Sandbox...") -ForegroundColor Green
    Invoke-Item $wsblocation
    #Wait for Windows Sandbox to start and delete the sandbox config file
    Start-Sleep -Seconds 5
    Remove-Item -Force:$true -Confirm:$false -Path $wsblocation
    Write-Host ("Done!") -ForegroundColor Green
}

Adding the Start-Sandbox function to the PowerShell profile

To make the Start-Sandbox function load automatically every time you open a new PowerShell you can add it to your PowerShell profile.

1. Open PowerShell as administrator then run the below command to create a PowerShell profile.

New-Item -Type File -Path $profile -Force
Directory: C:\Users\mpnadmin\Documents\WindowsPowerShell


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         5/28/2024   9:51 AM              0 Microsoft.PowerShell_profile.ps1

2. Open the newly created PowerShell profile using the Notepad.

notedpad.exe $profile

3. Adding the following line into the $profile. Don’t forget to change the path of the PowerShell script to fit with yours

. "D:\scripts\Start-Sandbox.ps1"
UQVuQRelyjTwWgc6zuxWQRYlJQEZpqMWHrhvn86lXA3T37udQR8Q5k7SBlMW

Using the Start-Sandbox function

From now on, to start a new Windows Sandbox instance, you just need to run the below command. When running the command without any parameters, the instance will start with the default configuration.

Start-Sandbox

The command below will start a new Windows Sandbox instance with 8GB of RAM.

Start-Sandbox -MemoryInMB '8192' 

And if you want to start an instance with 8GB of RAM and automatically map a folder from the host to the instance. Execute the command below:

Start-Sandbox -MappedFolder 'D:\temp' -MappedFolderWriteAccess -MemoryInMB '8192'

In the example below, I use the following command line to start an 8Gb RAM Windows Sandbox, printers connected, D:\temp directory connected in Read-Write mode, and an automatically started the Notepad.

Start-Sandbox `
    -MappedFolder 'D:\temp' `
    -MappedFolderWriteAccess `
    -PrinterRedirectionEnable `
    -MemoryInMB '8192' `
    -LogonCommand 'C:\Windows\system32\notepad.exe'
ADVERTISEMENT

Not a reader? Watch this related video tutorial:

5/5 - (1 vote)
Previous Post

How to Hide or Disable Breaking Change Warning Messages in Azure PowerShell

Next Post

How To Stop Teams from Changing from Available to Away Status Automatically

Related Posts

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024
Ftr19

How to Fix Windows Cannot Read the ProductKey From the Unattend Answer File in VirtualBox

July 26, 2024
Ftr25

How to Update Windows Terminal in Windows 10/11

July 26, 2024

How to Disable The Beep Sound in WSL Terminal on Windows

July 26, 2024

Leave a Reply Cancel reply

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

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory