I am trying to write a function to detect my default WSL installation.
function Get-WslDefault {
$installs = (wsl --list -v)
foreach ($s in $installs) {
if ($s.StartsWith("*")) {
return $s.Split(' ')[1]
}
}
return ""
}
This works in PS 7.2, but in Windows Powershell 5.1 (which I have to support) the strings in $installs look like they have been incorrectly translated from UTF16 - there is an unexpected space between each character. e.g.
N A M E S T A T E V E R S I O N
instead of
NAME STATE VERSION
I have tried every possible combination of $OutputEncoding and [Console]::OutputEncoding and [Console]::InputEncoding and nothing seems to fix the problem.
Has anyone come across this and is there a workaround?