Skip to content

Instantly share code, notes, and snippets.

@Trucido
Created March 6, 2019 03:59
Show Gist options
  • Save Trucido/17fe25b1963ed01b2b40f951a45fe7b8 to your computer and use it in GitHub Desktop.
Save Trucido/17fe25b1963ed01b2b40f951a45fe7b8 to your computer and use it in GitHub Desktop.
## Get-WSLPaths
<#
.SYNOPSIS
Gets paths and names of WSL distributions installed from Windows Store.
If no parameters supplied, it prints all installed WSL distributions.
.EXAMPLE
PS> Get-WSLPaths debian
PackageFullName : TheDebianProject.DebianGNULinux_1.1.6.0_x64__76v4gfsz19hv4
InstallLocation : C:\Program Files\WindowsApps\TheDebianProject.DebianGNULinux_1.1.6.0_x64__76v4gfsz19hv4
rootfsPSPath : ${env:LOCALAPPDATA}\Packages\TheDebianProject.DebianGNULinux_76v4gfsz19hv4\LocalState\rootfs
rootfsPath : C:\Users\User\AppData\Local\Packages\TheDebianProject.DebianGNULinux_76v4gfsz19hv4\LocalState\rootfs
#>
#Requires -Version 5
#Requires -Module Appx
function Get-WSLPaths
{
[CmdletBinding()]
param([string][parameter(Mandatory=$false,ValueFromPipeline=$True)]$DistroName)
Set-StrictMode -Version 'Latest'
# Packages location (Literal string for printing output)
$packagesLiteralPath = [string]'${env:LOCALAPPDATA}\Packages'
# Resolved packages location
$packagesPath = (Resolve-Path "${env:LOCALAPPDATA}\Packages").Path
# Sub-directory containing rootfs
$rootfsRelativePath = 'LocalState\rootfs'
if ($DistroName)
{
$distroPackages = Get-AppxPackage | Where-Object{$_.PackageFullName -imatch $DistroName}
}
else
{
# List of known WSL distros from Windows Store
$distros = @(
"TheDebianProject",
"KaliLinux",
"openSUSELeap42",
"openSUSELeap15",
"SUSELinuxEnterpriseServer12",
"SUSELinuxEnterpriseServer15",
"UbuntuonWindows",
"Ubuntu18.04onWindows",
# Third-Party published WSL distros
"WhitewaterFoundryLtd.Co.16571368D6CFF", # Debian fork
"WhitewaterFoundryLtd.Co.FedoraRemixforWSL", # Fedora fork
"WhitewaterFoundryLtd.Co.WLinuxEnterprise" # Scientific fork
)
$distroPackages = Get-AppxPackage -User "$env:USERNAME" | Where{$_ | Select-String ($distros.split()-join('|'))}
}
[scriptblock]$distroProperties = {
if ($null -ne $distroPackages)
{
foreach($distro in ($distroPackages))
{
$rootfsPSPath = [io.path]::combine($PackagesLiteralPath,
$distro.PackageFamilyName,
$rootfsRelativePath)
$rootfsPath = [io.path]::combine($PackagesPath,
$distro.PackageFamilyName,
$rootfsRelativePath)
Add-Member -InputObject $distro -MemberType NoteProperty -Name rootfsPSPath -Value $rootfsPSPath
Add-Member -InputObject $distro -MemberType NoteProperty -Name rootfsPath -Value $rootfsPath
$distro | Select-Object -Property PackageFullName, InstallLocation, rootfsPSPath, rootfsPath
}
}
}
Invoke-Command $distroProperties | Format-List # TODO: psformat xml to query as objects instead of spewing out string tables.
}
# If this script is invoked directly as opposed to being dot-sourced;
# invoke the embedded function relaying any arguments passed
if ($MyInvocation.ExpectingInput)
{
# True if we have ValueFromPipeLine $input (ExpectingInput)
$input | Get-WSLPaths @args
}
elseif (-not ($MyInvocation.InvocationName -eq '.' -or $MyInvocation.Line -eq ''))
{
# True if not dot-sourced or is invoked directly (empty invocation line)
Get-WSLPaths @args
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment