Skip to content

Instantly share code, notes, and snippets.

@KD-MM2
Last active September 26, 2025 01:06
Show Gist options
  • Select an option

  • Save KD-MM2/e0464f75256330d96bfc0e0874c06740 to your computer and use it in GitHub Desktop.

Select an option

Save KD-MM2/e0464f75256330d96bfc0e0874c06740 to your computer and use it in GitHub Desktop.
Attaching USB Device to WSL

Eg: attaching an arduino from host machine to wsl 2 linux machine

  1. Download and install usbipd-win

  2. Check the list of usb devices, and get the bus id(X-Y):

usbipd list
  1. Binding(eg: your device's bus id: 4-4):
usbipd bind --busid <bus-id>
  1. Attach it to WSL
usbipd attach --wsl --busid <bus-id>

4.1. Check for connected device in WSL:

lsusb

or

ls /dev/tty*
  1. To detach:
usbipd detach --busid <bus-id>

I also wrote a PowerShell script to automate these steps. See wsl-usb-binder.ps1.

# Ensure you run PowerShell with admin
function Get-UsbDevices {
$output = & "C:\Program Files\usbipd-win\usbipd.exe" list
$devices = @()
$lines = $output -split "`n"
for ($i = 2; $i -lt $lines.Length; $i++) {
$parts = $lines[$i] -split '\s+'
if ($parts.Length -ge 3) {
$devices += [PSCustomObject]@{
BusId = $parts[0]
Description = $parts[2..($parts.Length - 1)] -join ' '
State = $parts[1]
}
}
}
return $devices
}
function Attach-UsbDevice {
param (
[string]$busid
)
& "C:\Program Files\usbipd-win\usbipd.exe" bind --busid $busid
& "C:\Program Files\usbipd-win\usbipd.exe" attach --wsl --busid $busid
}
function Detach-UsbDevice {
param (
[string]$busid
)
& "C:\Program Files\usbipd-win\usbipd.exe" detach --busid $busid
}
function Monitor-Device {
param (
[string]$busid
)
while ($true) {
Start-Sleep -Seconds 1
$stateJson = & "C:\Program Files\usbipd-win\usbipd.exe" state
$device = ($stateJson | ConvertFrom-Json).Devices | Where-Object { $_.BusId -eq $busid }
if ($device.ClientIPAddress -eq $null) {
Write-Host "Reattaching device $($device.Description)..."
Attach-UsbDevice $busid
}
}
}
function Show-Menu {
param (
[array]$devices,
[array]$activeDevices,
[int]$selectedIndex
)
Clear-Host
Write-Host "Select USB Device (Press Enter to toggle, Q to quit):"
for ($i = 0; $i -lt $devices.Count; $i++) {
if ($i -eq $selectedIndex) {
Write-Host "-> $($devices[$i].BusId) - $($devices[$i].Description) ($($devices[$i].State))"
} else {
Write-Host " $($devices[$i].BusId) - $($devices[$i].Description) ($($devices[$i].State))"
}
}
Write-Host "`nActive Devices:"
foreach ($device in $activeDevices) {
Write-Host " $($device.BusId) - $($device.Description)"
}
}
function Main {
$devices = Get-UsbDevices
$activeDevices = @()
$selectedIndex = 0
while ($true) {
Show-Menu -devices $devices -activeDevices $activeDevices -selectedIndex $selectedIndex
$key = [Console]::ReadKey($true)
if ($key.Key -eq 'UpArrow') {
$selectedIndex = ($selectedIndex - 1 + $devices.Count) % $devices.Count
} elseif ($key.Key -eq 'DownArrow') {
$selectedIndex = ($selectedIndex + 1) % $devices.Count
} elseif ($key.Key -eq 'Enter') {
$selectedDevice = $devices[$selectedIndex]
if ($activeDevices -contains $selectedDevice) {
# Deactivate device
Detach-UsbDevice $selectedDevice.BusId
$activeDevices = $activeDevices | Where-Object { $_ -ne $selectedDevice }
} else {
# Activate device
Attach-UsbDevice $selectedDevice.BusId
$activeDevices += $selectedDevice
}
} elseif ($key.Key -eq 'Q') {
break
}
}
# Deactivate all active devices before exiting
foreach ($device in $activeDevices) {
Detach-UsbDevice $device.BusId
}
}
# Start
Main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment