PSWsl

Interact with WSL over PowerShell!

MIT License

Stars
46

PSWsl

PSWsl is a PowerShell module for interacting with Windows Subsystem for Linux (WSL). If PowerShell Core exists on your WSL distibution, you can easily invoke commands that return deserialized objects:

In PowerShell Core on Windows

PS > $versionHashTable = Invoke-WslCommand -Distribution debian -Command '$PSVersionTable'
PS > $versionHashTable.Platform
Unix

Demo:

This module simply wraps the wsl.exe, wslconfig.exe, and all the distribution.exe's in a more PowerShell friendly/specific way.

Why?

  • Quickly test scripts and modules against linux without having to stand up a VM or container.
  • Write multi-OS scripts that leverage PowerShell on Windows while also interacting with a Linux environment.
  • Interact with Linux-only tools and utilities with PowerShell.

Installation

Note: To get PowerShell Core on your WSL distribution, follow the steps here.

PSWsl is available on the PowerShell Gallery:

Install-Module PSWsl

You can also grab the latest zip from GitHub and put that in your $env:PSModulePath under a PSWsl folder.

Usage

If you don't have PSWsl in your PSModulePath, you will need to import it manually with:

Import-Module path/to/your/PSWsl/PSWsl.psd1

From there you should be able to enter into PSSessions in WSL, invoke commands from your current session of PowerShell, get your distributions, and set the default distribution:

Enter into a PSSession in a WSL distribution

# use your default distribution
Enter-WslDistribution

# be specific
Enter-WslDistribution -DistributionName ubuntu

Invoke commands within a WSL distribution

# You can use a string command, script block, or file. You can also pipe in a distribution array
PS > $versionHashTable = Invoke-WslCommand -Distribution debian -ScriptBlock { $PSVersionTable }
PS > $versionHashTable.Platform
Unix

Get your distributions

# Get all
Get-WslDistribution

# Supports wildcards
Get-WslDistribution -DistributionName ubunt*

# Piping
Get-WslDistribution | Invoke-WslCommand -Command '$PSVersionTable'

Set the default distribution

# explicit
Set-DistributionDefault -DistributionName ubuntu

# Piping
Get-WslDistribution -DistributionName ubun* | Set-DistributionDefault

Running the tests

The Pester tests use mocks to mock specific executables so you shouldn't need any setup.

Just run Invoke-Pester from the test directory.