Skip to content
Integrate Linux commands into Windows with PowerShell and the Windows Subsystem for Linux (WSL).
PowerShell
Branch: master
Clone or download
Latest commit 8da4759 Oct 15, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github/ISSUE_TEMPLATE
LICENSE Initial commit Sep 16, 2019
README.md
WslInterop.Tests.ps1 Added another failure condition for resolving Windows paths. Oct 16, 2019
WslInterop.psd1 Updated the module version. Oct 15, 2019
WslInterop.psm1

README.md

PowerShell WSL Interop

The Windows Subsystem for Linux (WSL) enables calling Linux commands directly within PowerShell via wsl.exe (e.g. wsl ls). While more convenient than a full context switch into WSL, it has the following limitations:

  • Prefixing commands with wsl is tedious and unnatural
  • Windows paths passed as arguments don't often resolve due to backslashes being interpreted as escape characters rather than directory separators
  • Windows paths passed as arguments don't often resolve due to not being translated to the appropriate mount point within WSL
  • Default parameters defined in WSL login profiles with aliases and environment variables aren’t honored
  • Linux path completion is not supported
  • Command completion is not supported
  • Argument completion is not supported

The Import-WslCommand function addresses these issues in the following ways:

  • By creating PowerShell function wrappers for commands, prefixing them with wsl is no longer necessary
  • By identifying path arguments and converting them to WSL paths, path resolution is natural and intuitive as it translates seamlessly between Windows and WSL paths
  • Default parameters are supported by $WslDefaultParameterValues similar to $PSDefaultParameterValues
  • Environment variables are supported by $WslEnvironmentVariables
  • Command completion is enabled by PowerShell's command completion
  • Argument completion is enabled by registering an ArgumentCompleter that shims bash's programmable completion

The commands can receive both pipeline input as well as their corresponding arguments just as if they were native to Windows.

Additionally, they will honor any default parameters defined in a hash table called $WslDefaultParameterValues similar to $PSDefaultParameterValues. For example:

$WslDefaultParameterValues["grep"] = "-E"
$WslDefaultParameterValues["less"] = "-i"
$WslDefaultParameterValues["ls"] = "-AFh --group-directories-first"

If you use aliases or environment variables within your login profiles to set default parameters for commands, define a hash table called $WslDefaultParameterValues within your PowerShell profile and populate it as above for a similar experience.

Environment variables can also be set in a hash table called $WslEnvironmentVariables using the pattern $WslEnvironmentVariables["<NAME>"] = "<VALUE>".

The import of these functions replaces any PowerShell aliases that conflict with the commands.

Usage

  • Install PowerShell Core
  • Install the Windows Subsystem for Linux (WSL)
  • Install the WslInterop module with Install-Module WslInterop
  • Import commands with Import-WslCommand either from your profile for persistent access or on demand when you need a command (e.g. Import-WslCommand "awk", "emacs", "grep", "head", "less", "ls", "man", "sed", "seq", "ssh", "tail", "vim")
  • (Optionally) Define a hash table called $WslDefaultParameterValues and set default arguments for commands using the pattern $WslDefaultParameterValues["<COMMAND>"] = "<ARGS>"
  • (Optionally) Define a hash table called $WslEnvironmentVariables and set environment variables using the pattern $WslEnvironmentVariables["<NAME>"] = "<VALUE>"

Known Issues

You can’t perform that action at this time.