Skip to content

a script to make Windows executables and PowerShell scripts executable in WSL bash without a filename extension

License

Notifications You must be signed in to change notification settings

ajkessel/wsltools

Repository files navigation

Quickstart

Just clone this repository (git clone https://github.com/ajkessel/wsltools.git) and then run install.sh as root.

Background

wsl-wrapper allows you to execute Windows commands in WSL with proper pathname parsing.

For example, if notepad.exe is in your path, with this solution, you can execute:

notepad.exe ~/readme.txt

...causing notepad to open and correctly find readme.txt in your WSL home directory, rather than throw an error because it doesn't know about ~.

This wrapper should also allow you to specify a number of parameters, including command-line switches and multiple filenames, for example:

gvim.exe -o ~/file1.txt ~/path/file2.txt

This should open the Windows gvim.exe executable with a split-pane view and both file1.txt and path/file2.txt from your WSL home directory open.

Installation

[interop]
enabled=true
appendWindowsPath=false
  • Copy wsl.conf from this repository to /etc/binfmt.d
  • Copy wsl-wrapper to /usr/local/bin
  • Make sure the file is world-readable/executable (chmod a+rx /usr/local/bin/wsl-wrapper)
  • Reload binfmt configuration (either sudo systemctl restart systemd-binfmt or just restart WSL instance wsl --shutdown from PowerShell)

PowerShell

You can use this same technique to make PowerShell scripts executable just from their filenames in WSL with the wrapper powershell-wrapper. Follow these steps:

  • Copy powershell.conf to /etc/binfmt.d
  • Copy powershell-wrapper to /usr/local/bin/
  • Make sure the file is world-readable/executable (chmod a+rx /usr/local/bin/powershell-wrapper)
  • Reload binfmt or restart WSL

You should then be able to execute a PowerShell just by name (e.g., ~/myscript.ps1 argument1 argument2) and it will work from WSL.

As currently implemented, the PowerShell wrapper does its best to find a Windows PowerShell executable, and if that fails, it tries to find a PowerShell executable. Feel free to hardcode $powerExe in the script with your path preference.

Office

Same deal with Microsoft Office files via office-wrapper. Follow these steps:

  • Copy office.conf to /etc/binfmt.d
  • Copy office-wrapper to /usr/local/bin/
  • Make sure the file is world-readable/executable (chmod a+rx /usr/local/bin/office-wrapper)
  • Reload binfmt or restart WSL

You should then be able to open a Microsoft Office document just by name (e.g., ~/docs/mydocument.docx or /mnt/c/documents/myspreadsheet.xlsx) and it will work from WSL.

wslshim

wslshim is a script that will symlink a Windows executable into ~/.local/bin without the filename extension. So, for example, if you run:

wslshim notepad

...it will locate notepad.exe and create a symlink to it in ~/.local/bin/notepad.

You can optionally use wslshim to map filename arguments to the executable, similar to the wrapper method above, by specifying -f with the invocation. But the wsl-wrapper method renders that unnecessary.

Troubleshooting

If you see this message in /var/log/syslog:

systemd[1]: systemd-binfmt.service - Set Up Additional Binary Formats was skipped because of an unmet condition check (ConditionVirtualization=!wsl).

You can fix it by adding this line to /etc/wsl.conf:

[boot]
protectBinFmt=false

Or alternatively by removing /usr/lib/systemd/system/systemd-binfmt.service.d/wsl.conf (or commenting out the ConditionVirtualization line therein), and then restart WSL.

I have filed an issue on WSL to try to figure out what's going on with that as it only recently started happening.

TODO

If there is enough interest in this, I may package it up as a Debian/Ubuntu package for easier installation.

About

a script to make Windows executables and PowerShell scripts executable in WSL bash without a filename extension

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages