How to List Installed WSL Distributions in Batch Script
Once the Windows Subsystem for Linux (WSL) is set up, a user can install multiple "Distributions" (distros) such as Ubuntu, Debian, Kali, or Alpine. For developers and system administrators, being able to programmatically list which distros are present on a machine, and check their running status, is essential for orchestrating multi-container environments or verifying a workstation's setup. Using a Batch script to pull these lists allows you to automate health checks and ensure that critical environments are correctly installed.
This guide explains how to use the wsl --list command and its various flags to extract this data.
Why List WSL Distributions?
- Auditing: Creating a report of which Linux versions are currently active on a system.
- Workflow Automation: Verifying that a specific distro (e.g., "Ubuntu") is present before attempting to run a script inside it.
- Troubleshooting: Identifying which distro is the "Default" and which version (WSL1 vs WSL2) they are using.
The base command for management is wsl.exe. When using it from a Batch script, you can capture its output using a FOR loop for further processing.
Method 1: Basic Human-Readable List
The most common way to see everything is the --list (or -l) command.
@echo off
echo [STATUS] Listing all installed WSL distributions...
echo.
wsl --list --verbose
pause
Note: The --verbose flag is highly recommended as it shows the current state (Running/Stopped) and the WSL version.
Method 2: Checking for a Specific Distribution
If you need to verify that a particular distribution is installed, use the registry to avoid the UTF-16 encoding issues that wsl --list produces in Batch scripts.
@echo off
setlocal EnableDelayedExpansion
set "TARGET=Ubuntu"
echo [PROCESS] Searching for %TARGET%...
:: Query the registry for installed distributions
set "FOUND=0"
for /f "tokens=*" %%k in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Lxss" /s /v DistributionName 2^>nul ^| findstr /i "%TARGET%"') do (
set "FOUND=1"
)
if "!FOUND!"=="1" (
echo [SUCCESS] %TARGET% is installed.
) else (
echo [WARNING] %TARGET% distro not found.
)
pause
Creating an Inventory Report Script
A professional script can aggregate the distribution data into a clean report format for IT documentation.
@echo off
setlocal EnableDelayedExpansion
echo ============================================================
echo WSL Environment Inventory
echo ============================================================
:: 1. Check if WSL is even installed
where wsl.exe >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] WSL is not installed.
pause
exit /b 1
)
:: 2. Display verbose list directly (best for human reading)
echo.
echo Current Distributions:
echo ------------------------------------------------------------
wsl --list --verbose
echo ------------------------------------------------------------
:: 3. List distribution names from the registry (reliable for scripts)
echo.
echo Registered distributions (from registry^):
echo ------------------------------------------------------------
set "COUNT=0"
for /f "tokens=3" %%a in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Lxss" /s /v DistributionName 2^>nul ^| findstr /i "DistributionName"') do (
echo %%a
set /a "COUNT+=1"
)
if !COUNT! equ 0 (
echo No distributions found.
)
echo ------------------------------------------------------------
echo Total: !COUNT! distribution(s^)
echo ============================================================
pause
Common Pitfalls and How to Avoid Them
Encoding "Ghost" Spaces
The wsl --list command often outputs text in UTF-16 Little Endian. When Batch tries to parse this directly in a FOR loop, you might get "n u l l" characters or spaces between every letter.
Wrong Way:
for /f %%a in ('wsl -l -q') do echo %%a
:: Result will be corrupted text with spaces between each character
Correct Way: Use the registry to read distribution names (as shown in Method 2 and the Inventory Script), or call PowerShell to convert the encoding:
for /f "tokens=*" %%a in ('powershell -NoProfile -Command "wsl -l -q"') do echo %%a
Listing "Available" vs "Installed"
Don't confuse wsl --list with wsl --list --online.
-l: Shows what is already on your machine.-l -o: Shows what is available for download from the Microsoft Store.
Best Practices for WSL Management
- Check Version: Always use
--verboseto ensure you aren't trying to run WSL2 commands on a distro that is accidentally still running on the legacy WSL1 engine. - Filter for "Running": Use
wsl -l -v | findstr "Running"to identify active Linux instances that might be consuming system RAM. - User Isolation: Remember that the list of distros is unique to each Windows user account. Listing distros as an "Administrator" might return an empty list even if the standard user has four distros installed.
The distribution marked with * is the one that opens when you simply type wsl without any arguments. You can change this via script using wsl --set-default <Name>.
Conclusion
Listing installed WSL distributions via Batch script is a fundamental requirement for managing modern, hybrid development environments. By utilizing the various flags of the wsl command, you can build reliable inventory tools and intelligent setup scripts that adapt to the specific Linux flavors present on a machine. This professional approach to system auditing ensures that your cross-platform workflows remain consistent, documented, and free from the confusion of "missing dependency" errors across your infrastructure.