Created
October 1, 2019 21:18
-
-
Save lzybkr/80c66be2bd866a2e7387b83045e0ab81 to your computer and use it in GitHub Desktop.
Hack to enable FOO=bar syntax in PowerShell
This file contains hidden or 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
$ExecutionContext.InvokeCommand.CommandNotFoundAction = | |
{ | |
param([string]$commandName, | |
[CommandLookupEventArgs]$eventArgs) | |
if ($commandName -match "(.*)=(.*)") { | |
$var = $matches[1] | |
if ($var.StartsWith("get-")) { | |
$var = $var.Substring(4) | |
} | |
$val = $matches[2] | |
$eventArgs.CommandScriptBlock = { | |
try { | |
$backup = Get-Item env:$var -ea Ignore | |
Set-Item env:$var $val | |
$rest = $args[1..$($args.Length - 1)] | |
& $args[0] @rest | |
} finally { | |
Set-Item env:$var $backup | |
} | |
}.GetNewClosure() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment