How to set environment variable and execute command in one line (PowerShell)?

Question:

How can I set value of environment variable and execute command in one line in PowerShell?

I have this:

and it’s working great in Linux.

How can I translate above to PowerShell command which will work on Windows Server 2012 – Windows 10?

I tried:

and I get error: “The & is reserved for future use…” – Windows 2012.

Answer:

Try this:

In reality this is two commands separated by a semi-colon so they can be run as a single line.

Note also that from PowerShell you set environment variables via $env:<name of var>.

Source:

How to set environment variable and execute command in one line (PowerShell)? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply