简体   繁体   中英

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

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

I have this:

PGPASSWORD=db_pass psql -U db_user -d db_name -a -h localhost -f some.sql

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:

SET "PGPASSWORD=db_pass" & "C:\Program Files (x86)\PostgreSQL\9.4\bin\psql.exe" -U postgres -a -d db_name -h localhost -f some.sql

and I get error: "The & is reserved for future use..." - Windows 2012.

Try this:

$env:PGPASSWORD='db_pass'; & "C:\Program Files (x86)\PostgreSQL\9.4\bin\psql.exe" -U postgres -a -d db_name -h localhost -f some.sql

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> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM