Created
February 26, 2018 04:42
-
-
Save JPablomr/e8ea600de021e39759c7f19c7bccc866 to your computer and use it in GitHub Desktop.
Run WSL seamlessly from powershell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Windows PowerShell Cookbook is AWESOME | |
# Turns out you can hook into every aspect of the invocation of a CLI command, | |
# so when I run a command that's not in powershell, it will also try to run it in WSL | |
$ExecutionContext.SessionState.InvokeCommand.CommandNotFoundAction = { | |
param($CommandName, $CommandLookupEventArgs) | |
# There was a get- that prefixes the command | |
$realCommand = $CommandName -replace "^get-","" | |
# Probably more unsafe things, but I can't think of any | |
$unsafeCommands = @("rm") | |
# This scriptblock gets executed | |
$CommandLookupEventArgs.CommandScriptBlock = { | |
if ($realCommand -notin $unsafeCommands) { | |
# Clean up slashes for linux | |
$realArgs = (($args -replace "C:\\","/mnt/c/") -replace "\\","/") | |
# And run in bash | |
bash -c "$realCommand $realArgs" | |
} | |
}.GetNewClosure() | |
# Not strictly necessary but it was in the cookbook. | |
$CommandLookupEventArgs.StopSearch = $true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment