skip to content

WSL Interoperability

Running Linux tools from Windows and vice versa, file system access, and networking between WSL and Windows.

2 min read 5 snippets 3d ago

WSL Interoperability#

Run Linux commands from PowerShell#

# Run a Linux command directly
wsl ls -la /home/jay

# Pipe Windows command into Linux
Get-Content .\data.csv | wsl awk -F, '{ print $2 }'

# Use Linux grep on Windows output
ipconfig | wsl grep "IPv4"

# Run a specific distro
wsl -d Ubuntu-22.04 -- cat /etc/os-release

Run Windows commands from WSL#

# Call Windows executables (the .exe is required)
notepad.exe README.md
explorer.exe .
clip.exe < ~/.ssh/id_ed25519.pub   # copy SSH pubkey to clipboard

# Open Windows browser from WSL
cmd.exe /c start https://example.com

# Run PowerShell from WSL
powershell.exe -Command "Get-Date"
pwsh.exe -Command "Write-Host 'PowerShell 7'"

File system paths#

# Access Windows drives from WSL
ls /mnt/c/Users/Jay/Documents

# Windows path to WSL path
wslpath 'C:\Users\Jay\Documents'          # β†’ /mnt/c/Users/Jay/Documents
wslpath -w /home/jay/project              # β†’ \\wsl.localhost\Ubuntu\home\jay\project

# Access WSL files from Windows Explorer
# Type in address bar: \\wsl.localhost\Ubuntu\home\jay

Networking#

# Get Windows host IP from WSL (WSL2)
cat /etc/resolv.conf | grep nameserver | awk '{print $2}'
# or
ip route show default | awk '{print $3}'

# Expose WSL port to Windows (automatic in WSL2 for localhost)
# localhost:3000 in WSL β†’ localhost:3000 in Windows browser

# Forward WSL port to LAN (run in PowerShell as Admin)
netsh interface portproxy add v4tov4 `
  listenport=3000 listenaddress=0.0.0.0 `
  connectport=3000 connectaddress=127.0.0.1

Useful WSL management commands (PowerShell)#

# List installed distros
wsl --list --verbose

# Set default distro
wsl --set-default Ubuntu-22.04

# Shutdown all WSL instances
wsl --shutdown

# Export distro to backup
wsl --export Ubuntu-22.04 ubuntu-backup.tar

# Import from backup
wsl --import Ubuntu-Restored C:\WSL\Ubuntu ubuntu-backup.tar

# Set WSL version for a distro
wsl --set-version Ubuntu-22.04 2