Scott Hanselman

A wonderfully unholy alliance - Real Linux commands for PowerShell with WSL function wrappers

October 1, '19 Comments [10] Posted in Linux | PowerShell
Sponsored By

Dropdown filled with shells in the Windows TerminalI posted recently about What's the difference between a console, a terminal, and a shell? The world of Windows is interesting - and a little weird and unfamiliar to non-Windows people. You might use Ubuntu or Mac and you've picked your shell like zsh or bash or pwsh, but then you come to Windows and we're hopping between shells (and now operating systems with WSL!) on a tab by tab basis.

If you're using a Windows shell like PowerShell because you like it's .NET Core based engine and powerful scripting language, you might still miss common *nix shell commands like ls, grep, sed and more.

No matter what shell you're using in Windows (powershell, yori, cmd, whatever) you can always call into your default Ubuntu instance with "wsl command" so "wsl ls" or "wsl grep" but it'd be nice to make those more naturally and comfortably integrated.

Now there's a new series of "function wrappers" that make Linux commands available directly in PowerShell so you can easily transition between multiple environments.

This might seem weird but it allows us to create amazing piped commands that move in and out of Windows and Linux, PowerShell and bash. It's actually pretty amazing and very natural if you, like me, are non-denominational in your choice of operating system and preferred shell.

These function wrappers are very neatly designed and even expose TAB completion across operating systems! That means I can type Linux commands in PowerShell and TAB completion comes along!

It's super easy to set up. From Mike Battista's Github

  • 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")

You'll do your Install-Module just one, and then run notepad $profile and add just a that single last line. Make sure you change it to expose the WSL/Linux commands that you want. Once you're done, you can just open PowerShell Core and mix and match your commands!

From the blog, "With these function wrappers in place, we can now call our favorite Linux commands in a more natural way without having to prefix them with wsl or worry about how Windows paths are translated to WSL paths:"

  • man bash
  • less -i $profile.CurrentUserAllHosts
  • ls -Al C:\Windows\ | less
  • grep -Ein error *.log
  • tail -f *.log

It's a really genius thing and kudos to Mike for sharing it with us! Go try it now. https://github.com/mikebattista/PowerShell-WSL-Interop


Sponsor: Like C#? We do too! That’s why we've developed a fast, smart, cross-platform .NET IDE which gives you even more coding power. Clever code analysis, rich code completion, instant search and navigation, an advanced debugger... With JetBrains Rider, everything you need is at your fingertips. Code C# at the speed of thought on Linux, Mac, or Windows. Try JetBrains Rider today!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb
Friday, October 04, 2019 5:25:04 PM UTC
This is outstanding! I got so used to using `{some ps command} | wsl grep "blah"}`. Also, super thrilled about being able to use `ls -la` in pwsh.
Mike Paterson
Friday, October 04, 2019 11:05:21 PM UTC
I do this for personal use.

Unfortunately, too often I have to fix some hot-shot developer's idea that using a set of third party command line tools on the 24x7 production server is a good idea.

For production systems, build steps, data extracts, reports, etc all of which are production work, my team goes with Windows built-in commands with no add-ins and no third party executable code. It gets us 99% of the way there.

That 10 minute hack command line script, nuget package, third party command line tool saved you 1 hour 3 years ago will cost my team today days or weeks of diagnosis, troubleshooting and rework.

It's why you don't remove the engine from your newly purchased car and replace it with one from a jet airplane.

How can programmers not see this plain common sense notion?
bob
Saturday, October 05, 2019 1:03:58 PM UTC

That 10 minute hack command line script, nuget package, third party command line tool saved you 1 hour 3 years ago will cost my team today days or weeks of diagnosis, troubleshooting and rework.


A-men.
Raj Chaudhuri
Saturday, October 05, 2019 7:46:34 PM UTC
That's fucking good!

He, bob, sit down! :)
Laszlo Szoke
Sunday, October 06, 2019 5:15:48 AM UTC
Yes That is Good
Wednesday, October 09, 2019 5:00:23 PM UTC
Very good, thank you very much.
Thursday, October 10, 2019 8:13:03 AM UTC
(Some html is allowed: up4pc.com, b, blockquote@cite, em, i, li, ol, pre, strike, strong, sub, super, u, ul) where the @ means "up4pc.com" For example, you can use <a href="" up4pc.com=""> or
.
Thursday, October 10, 2019 8:13:30 AM UTC
https://up4pc.com/
Friday, October 11, 2019 3:16:50 PM UTC
I much prefer any variant of the Korn shell to bash.

The smallest implementation that I have found is the BSD-licensed mksh port in Cygwin. Just pull mksh.exe and cygwin1.dll out of the /bin, and run them on any Windows system.

Combining this with the Win32/64 port of busybox (using mklink flesh out busybox all its aliases), and this creates the smallest footprint for a UNIX userland - two programs and one library.

Busybox also contains several shells, but they are significantly lacking in functionality (arrays, coprocesses, etc.).
Charlie
Monday, October 14, 2019 5:17:38 PM UTC
Combining this with the Win32/64 port of busybox (using mklink flesh out busybox all its aliases), and this creates the smallest footprint for a UNIX userland
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.