Note: If you can assume that Node.js is already installed - as is by definition the case when you're invoking [TG40] - consider use of [TG41] helper packages, as shown in Cyril CHAPON helpful answer .
This answer focuses on generic solutions from within PowerShell.
TL; dr
# Set env. variable temporarily, invoke the external utility, remove / restore old value. $oldVal, $env:MYVAR = $env:MYVAR, 8000; node index.js; $env:MYVAR = $oldVal
Simply put, if there is no pre-existing MY_VAR value to be restored.
$env:MYVAR=8000; node index.js; $env:MYVAR=$null
See explanation and alternative based on helper function below.
In addition to Harikrishnan 's effective answer :
PowerShell does not have an equivalent for the media transfer method that the POSIX-like shell offers (starting with PowerShell v7 - however introducing it into PowerShell - not necessarily with the same syntax) discusses this issue of GitHub ); eg.:
In PowerShell, as the answer of Harikrishnan shows, you first need to define the environment variable, and then call the external program in a separate expression , so $env:MY_VAR="8000"; node index.js $env:MY_VAR="8000"; node index.js is the right PowerShell solution, but it costs nothing to keep $env:MY_VAR in scope for the remainder of the session (this is set at the process level ).
Note that even using the script block called with & to create a child region does not help here, since such child regions only apply to PowerShell variables, not environment variables.
Of course, you can remove the environment variable manually after calling node :
Remove-Item env:MY_VAR or even just $env:MY_VAR = $null , this is what the 1st command above shows.
A more structured alternative β perhaps better when setting multiple environment variables and / or calling multiple commands β is to use a script block called with & :
& { $oldVal, $env:MY_VAR = $env:MY_VAR, 8000; node index.js; $env:MY_VAR = $oldVal }
This takes advantage of:
{ ... } - a script block that provides a clearly visible grouping for teams in it; called with & , creates a local scope, so the auxiliary variable $oldVal automatically leaves the scope when it exits the block.
$oldVal, $env:MY_VAR = $env:MY_VAR, 8000 saves the old value (if any) from $env:MY_VAR to $oldVal when changing the value to 8000 ; This method of simultaneously assigning to multiple variables (called destructuring assignment in some languages) is explained in Get-Help about_Assignment_Operators , section " Get-Help about_Assignment_Operators MULTIPLE VARIABLES".
Alternatively , use the helper function described below , or use the try { ... } finally { ... } approach, as shown in this related mine answer .
Helper function for changing the environment in the command area.
If you defined a helper function below (remember that function definitions must be placed before they are called), you can achieve a change in the environment on the command line as follows:
# Invoke 'node index.js' with a *temporarily* set MY_VAR environment variable. Invoke-WithEnvironment @{ MY_VAR = 8000 } { node index.js }
Invoke-WithEnvironment() source code :
function Invoke-WithEnvironment { <