- Creating Batch Files to Switch to Specific Virtual Desktops Using PowerShell and the VirtualDesktop Tool
- Prerequisites
- Step 1: Ensure Eight Virtual Desktops Exist
- Step 2: Create a PowerShell Script to Generate Batch Files
- Step 3: Run the PowerShell Script
- Step 4: Add
C:\QuickAccess
to Your PATH Environment Variable - Step 5: Use the Batch Files to Switch Workspaces
#Creating Batch Files to Switch to Specific Virtual Desktops Using PowerShell and the VirtualDesktop Tool
This guide will help you create a PowerShell script that generates batch files (1.bat
to 8.bat
) to switch directly to any of your eight virtual desktops (workspaces) on Windows 11. We will use the VirtualDesktop command-line tool by Markus Scholtes to manage virtual desktops via command-line commands.
#Prerequisites
-
Download the VirtualDesktop Tool:
-
Access the VirtualDesktop repository on GitHub:
-
Download Precompiled Binaries:
-
Navigate to the Releases section:
-
Download the latest release ZIP file (e.g.,
VirtualDesktop_v1.19.zip
).
-
-
Extract the Executable:
-
Extract the contents of the ZIP file to a folder accessible to your user account, e.g.,
C:\Tools\VirtualDesktop
. -
Ensure you use the correct executable for your Windows version:
-
For Windows 11, use
VirtualDesktop11.exe
. -
For Windows 10, use
VirtualDesktop.exe
.
-
-
-
#Step 1: Ensure Eight Virtual Desktops Exist
We need to make sure that you have exactly eight virtual desktops. We will use PowerShell to create additional desktops if necessary.
-
Open PowerShell:
- Press Win + X and select Windows PowerShell or Windows Terminal.
-
Navigate to the VirtualDesktop Directory:
Set-Location "C:\Tools\VirtualDesktop"
-
Check the Current Number of Virtual Desktops:
$virtualDesktopExe = "C:\Tools\VirtualDesktop\VirtualDesktop11.exe" if (-Not (Test-Path -Path $virtualDesktopExe)) { Write-Error "VirtualDesktop executable not found at $virtualDesktopExe" exit 1 } $countOutput = & $virtualDesktopExe /Count $currentDesktopCount = ($countOutput -match 'Desktops: (\d+)') ? [int]$Matches[1] : 0 Write-Host "Current number of desktops: $currentDesktopCount"
-
Create Additional Desktops if Necessary:
for ($i = $currentDesktopCount + 1; $i -le 8; $i++) { & $virtualDesktopExe /New | Out-Null Write-Host "Created virtual desktop $i" }
-
Optionally, Name Your Desktops:
for ($i = 1; $i -le 8; $i++) { & $virtualDesktopExe /GetDesktop:$i /Name:"Workspace $i" | Out-Null Write-Host "Named desktop $i as 'Workspace $i'" }
#Step 2: Create a PowerShell Script to Generate Batch Files
We will create a PowerShell script that automatically generates the batch files needed to switch to each of the eight virtual desktops.
-
Create the Output Directory for Batch Files:
- We’ll use
C:\QuickAccess
as the folder to store the batch files.
$outputFolder = "C:\QuickAccess" if (-Not (Test-Path -Path $outputFolder)) { New-Item -ItemType Directory -Path $outputFolder -Force | Out-Null Write-Host "Created output directory: $outputFolder" }
- We’ll use
-
Create the PowerShell Script:
- Open Notepad or your preferred text editor.
-
Script Content:
Paste the following PowerShell script:
# Path to the VirtualDesktop executable
$virtualDesktopPath = "G:\05-portable\VirtualDesktop11-24H2.exe"
# Output folder for the batch files
$outputFolder = "C:\QuickAccess"
# Ensure the VirtualDesktop executable exists
if (-Not (Test-Path -Path $virtualDesktopPath)) {
Write-Error "VirtualDesktop executable not found at $virtualDesktopPath"
exit 1
}
# Ensure the output folder exists
if (-Not (Test-Path -Path $outputFolder)) {
New-Item -ItemType Directory -Path $outputFolder -Force | Out-Null
Write-Host "Created output directory: $outputFolder"
}
# Generate batch files for desktops 1 to 8
for ($desktopNumber = 1; $desktopNumber -le 8; $desktopNumber++) {
# Batch file content
$batFileContent = "@echo off`n"
$batFileContent += "`"$virtualDesktopPath`" /Switch:$desktopNumber"
# Batch file path
$batFilePath = Join-Path $outputFolder "$desktopNumber.bat"
# Create the batch file
try {
Set-Content -Path $batFilePath -Value $batFileContent -Encoding ASCII
Write-Host "Created batch file: $batFilePath"
}
catch {
Write-Error "Error creating batch file ${batFilePath}: $_"
# Or using the -f operator:
# Write-Error ("Error creating batch file {0}: {1}" -f $batFilePath, $_)
}
}
Write-Host "All batch files have been generated in $outputFolder."
Notes:
-
Replace
$virtualDesktopPath
with the actual path to your VirtualDesktop executable if it’s different. -
Ensure that the path to the output folder
$outputFolder
matches your desired location. -
The script includes error handling to alert you if the VirtualDesktop executable is not found or if there are issues creating the batch files.
-
Save the Script:
- Save the script as
GenerateBatchFiles.ps1
in a folder of your choice, e.g.,C:\Scripts
.
- Save the script as
#Step 3: Run the PowerShell Script
-
Run the Script:
-
Open PowerShell and navigate to the script’s directory:
Set-Location "C:\Scripts"
-
Execute the script:
.\GenerateBatchFiles.ps1
-
You should see output indicating that each batch file has been created.
-
-
Verify the Batch Files:
-
Navigate to
C:\QuickAccess
and confirm that1.bat
to8.bat
have been created. -
Open one of the batch files (e.g.,
1.bat
) to verify its content:@echo off "C:\Tools\VirtualDesktop\VirtualDesktop11.exe" /Switch:1
-
#Step 4: Add C:\QuickAccess
to Your PATH Environment Variable
Adding the C:\QuickAccess
folder to your PATH allows Windows to recognize the batch files when using the Run dialog or Command Prompt.
-
Modify User PATH Variable:
-
Press Win + X and select System (or Settings > System > About > Advanced system settings).
-
Click on Advanced system settings.
-
In the System Properties window, click Environment Variables.
-
Under User variables, select Path and click Edit.
-
Click New and add:
C:\QuickAccess
-
Click OK to save changes.
-
-
Restart Any Open Command Prompts or Applications:
- For the changes to take effect, restart any applications that might use the PATH variable.
#Step 5: Use the Batch Files to Switch Workspaces
You can now switch to any workspace by running the corresponding batch file.
-
From the Run Dialog:
-
Press Win + R to open the Run dialog.
-
Type the number of the workspace you want to switch to (e.g.,
1
for workspace 1) and press Enter.
-
-
From Command Prompt or PowerShell:
- Simply type the number (which corresponds to the batch file’s name) and press Enter:
C:\Users\YourUsername> 3
- Simply type the number (which corresponds to the batch file’s name) and press Enter:
#CODE
.\00-QuickAccess\0.bat
start "" "C:\Program Files\PowerShell\7\pwsh.exe"
.\00-QuickAccess\1.bat
@echo off
"G:\05-portable\VirtualDesktop11-24H2.exe" /Switch:0
.\00-QuickAccess\2.bat
@echo off
"G:\05-portable\VirtualDesktop11-24H2.exe" /Switch:1
.\00-QuickAccess\3.bat
@echo off
"G:\05-portable\VirtualDesktop11-24H2.exe" /Switch:2
.\00-QuickAccess\4.bat
@echo off
"G:\05-portable\VirtualDesktop11-24H2.exe" /Switch:3
.\00-QuickAccess\5.bat
@echo off
"G:\05-portable\VirtualDesktop11-24H2.exe" /Switch:4
.\00-QuickAccess\6.bat
@echo off
"G:\05-portable\VirtualDesktop11-24H2.exe" /Switch:5
.\00-QuickAccess\7.bat
@echo off
"G:\05-portable\VirtualDesktop11-24H2.exe" /Switch:6
.\00-QuickAccess\8.bat
@echo off
"G:\05-portable\VirtualDesktop11-24H2.exe" /Switch:7
.\00-QuickAccess\9.bat
@echo off
"G:\05-portable\VirtualDesktop11-24H2.exe" /Switch:8
.\00-QuickAccess\b.bat
@echo off
pushd "G:\05-portable\ungoogled"
start "" "C:\Program Files\PowerShell\7\pwsh.exe" -NoProfile -ExecutionPolicy Bypass -File "un-script.ps1"
exit
.\00-QuickAccess\t.bat
start "" "G:\05-portable\ExplorerPlusPlus\Explorer++.exe"