Skip to content

Instantly share code, notes, and snippets.

@lzybkr
Created October 1, 2019 21:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lzybkr/80c66be2bd866a2e7387b83045e0ab81 to your computer and use it in GitHub Desktop.
Save lzybkr/80c66be2bd866a2e7387b83045e0ab81 to your computer and use it in GitHub Desktop.
Hack to enable FOO=bar syntax in PowerShell
$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