We can set variables for a single command using this syntax: VAR1=VALUE1 VAR2=VALUE2 ... Command ARG1 ARG2...
To set the environmental variable using PowerShell you need to use the assignment operator (=). If the variable already exists then you can use the += operator to append the value, otherwise, a new environment variable will be created.
To set an environment variable, use the command " export varname=value ", which sets the variable and exports it to the global environment (available to other processes). Enclosed the value with double quotes if it contains spaces. To set a local variable, use the command " varname =value " (or " set varname =value ").
The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.
Generally, it would be better to pass info to the script via a parameter rather than a global (environment) variable. But if that is what you need to do you can do it this way:
$env:FOO = 'BAR'; ./myscript
The environment variable $env:FOO can be deleted later like so:
Remove-Item Env:\FOO
I got motivated enough about this problem that I went ahead and wrote a script for it: with-env.ps1
Usage:
with-env.ps1 FOO=foo BAR=bar your command here
# Supports dot-env files as well
with-env.ps1 .\.env OTHER_ENV=env command here
On the other hand, if you install Gow you can use env.exe
which might be a little more robust than the quick script I wrote above.
Usage:
env.exe FOO=foo BAR=bar your command here
# To use it with dot-env files
env.exe $(cat .env | grep.exe -v '^#') SOME_OTHER_ENV=val your command
2 easy ways to do it in a single line:
$env:FOO='BAR'; .\myscript; $env:FOO=''
$env:FOO='BAR'; .\myscript; Remove-Item Env:\FOO
Just summarized information from other answers (thank you folks) which don't contain pure one-liners for some reason.
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.
You could do this by running the script as a Job:
Start-Job -InitializationScript { $env:FOO = 'BAR' } -FilePath .\myscript.ps1 |
Receive-Job -Wait -AutoRemoveJob
You could also pass arguments to the script, using the ArgumentList
parameter of Start-Job
:
$jobArgs = @{
InitializationScript = { $env:FOO = 'BAR' }
FilePath = '.\myscript.ps1'
ArgumentList = 'arg1', 'arg2'
}
Start-Job @jobArgs | Receive-Job -Wait -AutoRemoveJob
Advantages and disadvantages
try
/ finally
to do it correctly even in the presence of exceptions).Write-Output
to communicate back to the main script. This could be an advantage or a disadvantage, depending on the use case.Making a 'subshell' by invoking powershell with a script block allows you to scope the changes to the environment:
pwsh -Command { $env:MYVAR="myvalue"; .\path\to.exe }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With