简体   繁体   English

在PowerShell中,如何在运行脚本之前设置$ HOME变量?

[英]In PowerShell, how do I set $HOME variable before running a script?

I am new to Windows PowerShell. 我是Windows PowerShell的新手。 Basically, before running a script in command line, I would like to set the $HOME environment variable to a different directory. 基本上,在命令行中运行脚本之前,我想将$ HOME环境变量设置为其他目录。

I don't really want to change the system $HOME variable that may affect other software. 我真的不想更改可能影响其他软件的系统$ HOME变量。 I just want this change to affect the script I am running or just the shell I am using. 我只希望此更改影响我正在运行的脚本或我正在使用的Shell。

I don't recommend changing $HOME but if you must: 我不建议更改$ HOME,但是如果您必须执行以下操作:

Set-Variable -Name Home -Value C:\Windows -Force

This will impact the shell/session and every script that runs in this shell. 这将影响外壳程序/会话以及在该外壳程序中运行的每个脚本。 If doing this for a script you might think about setting it back after the script finishes eg: 如果对脚本执行此操作,则可以考虑在脚本完成后将其重新设置,例如:

$oldHome = $Home
Set-Variable -Name Home -Value C:\windows -Force
...
Set-Variable -Name Home -Value $oldHome -Force

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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