To accomplish the equivalent of the Unix syntax, you not only have to set the environment variable, but you have to reset it to its former value after executing the command. I've accomplished this for common commands I use by adding functions similar to the following to my PowerShell profile.
function cmd_special()
{
$orig_master = $env:app_master
$env:app_master = 'http://host.example.com'
mycmd $args
$env:app_master = $orig_master
}
So mycmd
is some executable that operates differently depending on the value of the environment variable app_master
. By defining cmd_special
, I can now execute cmd_special
from the command line (including other parameters) with the app_master
environment variable set... and it gets reset (or even unset) after execution of the command.
Presumably, you could also do this ad-hoc for a single invocation.
& { $orig_master = $env:appmaster; $env:app_master = 'http://host.example.com'; mycmd $args; $env:app_master = $orig_master }
It really should be easier than this, but apparently this isn't a use-case that's readily supported by PowerShell. Maybe a future version (or third-party function) will facilitate this use-case. It would be nice if PowerShell had a cmdlet that would do this, e.g.:
with-env app_master='http://host.example.com' mycmd
Perhaps a PowerShell guru can suggest how one might write such a cmdlet.