2

I want to see if I'm running a particular wsl distribution (Windows 10 Home, WSL 2):

PS C:\Users\User> wsl --list --running
Windows Subsystem for Linux Distributions:
Ubuntu (Default)
MyDistro
PS C:\Users\User> wsl --list --running | Select-String -Pattern "MyDistro"
PS C:\Users\User>

No output. I used Get-Member to see that the output is a string; if I run it through something like | Out-String -stream it makes no difference.

I can get a match with Select-String . or Select-String .* but it matches everything, which isn't helpful.

Yes, I want to see if there's a running distro with a particular name. Is there a better way to do that in PowerShell?

Eric
  • 2,115
  • 2
  • 20
  • 29

1 Answers1

9

Inexplicably, wsl --list --running produces UTF-16LE-encoded ("Unicode"-encoded) output rather than respecting the console's (OEM) code page.

A quick-and-dirty workaround - which assumes that all running distros are described only with ASCII-range characters (which seems likely) - is to use the following:

(wsl --list --running) -replace "`0" | Select-String -Pattern MyDistro

A proper workaround that supports all Unicode characters requires more effort:

$prev = [Console]::OutputEncoding; [Console]::OutputEncoding = [System.Text.Encoding]::Unicode
wsl --list --running | Select-String -Pattern MyDistro
[Console]::OutputEncoding = $prev
mklement0
  • 382,024
  • 64
  • 607
  • 775