Today I'm just looking to check if a specific subsystem is installed on my windows workstation. So I'm using Windows Subsystem for Linux (WSL) and install Ubuntu available from Microsoft Store. Now i'm trying to have a way to check if it's installed in a programmatic manner.
I've this output:
PS C:\> wsl -l -v
NAME STATE VERSION
* Ubuntu-20.04 Stopped 2
And I need to know if "Ubuntu-20.04" is installed. I've tried many things, but nothing revelant like:
$state = wsl -l -v
if($state -like '*Ubuntu*') {
Write-Host 'Installed'
} else {
Write-Host 'Nope'
}
But not working. Do you have a clue for me ? Thanks everyone !
This seems to be an encoding issue. There are (invisible) null characters in your string. I'm still trying to find out what's the best way to deal with it.
In the meanwhile ... here's a quick fix:
$state = (wsl -l -v) -replace "\x00",""
UPDATE 1
Another workaround, but this will change the encoding for the entire session:
[System.Console]::OutputEncoding = [System.Text.Encoding]::Unicode
UPDATE 2
Slightly longer solution, reads the output as unicode (utf-16):
function Get-UnicodeOutput {
$fileName = $Args[0]
$arguments = ($Args | select -skip 1) -join ' '
$startInfo = [System.Diagnostics.ProcessStartInfo]::new($fileName, $arguments)
$startInfo.RedirectStandardOutput = $true
$startInfo.UseShellExecute = $false
$startInfo.StandardOutputEncoding = [System.Text.Encoding]::Unicode
$process = [System.Diagnostics.Process]::new()
$process.StartInfo = $startInfo
$process.Start() | Out-Null
$output = $process.StandardOutput.ReadToEnd()
$process.WaitForExit()
$process.Dispose()
return $output
}
$state = Get-UnicodeOutput wsl -l -v
Note: For sake of simplicity, this does not perform any error handling.