Run PowerShell command with environment variable

Unix-like shells typically have a syntax that allows running a command with one or more environment variables having values that don’t persist after the command. For example, in Unix-like shells:

CC=clang CXX=clang++ make

Runs the program “make” with environment variables CC and CXX temporarily set. Subsequent commands use the original value, if any, of CC and CXX.

In PowerShell this syntax doesn’t directly exist, but can be effected like:

pwsh -c { $env:CC="clang"; $env:CXX="clang++"; make }

Another example:

PowerShell:

pwsh -c { $env:hi="hello"; python -c "import os; print(os.getenv('hi'))" }

Unix-like shell:

hi="hello" python -c "import os; print(os.getenv('hi'))"