PowerShell子脚本更改父级环境

时间:2013-05-17 12:51:19

标签: shell powershell environment

我有更改当前工作目录的PS脚本:

Set-Location (Join-Path (Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Definition)) -ChildPath src)

不幸的是,这会影响父进程环境。因此,当我从终端调用此脚本时,将在脚本调用后更改当前目录。在Unix环境中,脚本只能更改本地当前目录,以及脚本的环境变量,而不能更改父shell。

如何更改此行为?

1 个答案:

答案 0 :(得分:1)

# Create a drive t: in the local scope whose root is c:\temp\backup
# drive t: disappears when the scope (function in this case) is exited
function test {
  New-PSDrive -name "t" -scope local -root c:\temp\backup -PSProvider filesystem; 
  ls t:
}
ls t: # fails unless t: already exists
test  # succeeds
ls t: # fails