如何在一行中设置环境变量并执行命令(PowerShell)?

时间:2018-01-09 08:23:45

标签: bash powershell environment-variables

如何在PowerShell中的一行中设置环境变量的值并执行命令?

我有这个:

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

它在Linux中运行良好。

如何将上面的内容转换为可在Windows Server 2012上运行的PowerShell命令 - Windows 10?

我试过了:

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

我收到错误:“& for for future use ...” - Windows 2012。

1 个答案:

答案 0 :(得分:3)

试试这个:

$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

实际上,这是两个用分号分隔的命令,因此它们可以作为一行运行。

另请注意,在PowerShell中,您可以通过$env:<name of var>设置环境变量。