首页 > 解决方案 > 执行脚本和点源一样吗?

问题描述

我最近在 powershell 中看到了一些令人惊讶的行为。

考虑脚本 c:\scripts\tst1.ps:

Set-location c:\

现在从powershell:

C:\scripts PS> .\tst1.ps1
C:\ PS>

为什么调用shell根据“set-location”变成了“C:\”?“set-location”不应该根据其他shell(如bash)的正常脚本规则从子shell运行吗?

我认为您必须“点源”脚本才能获得这种行为???那是: ”。.\tst1.ps1”?这里发生了什么。

是我的主机上的powershell配置错误还是这些天powershell的实际行为?

有没有办法重新配置 powershell 以获得我期望的旧行为,它类似于脚本在子 shell 中运行并退出并且当前目录不受影响的 bash shell?

标签: powershell

解决方案


您可以使用新的 powershell 实例运行脚本:

C:\scripts PS> powershell .\tst1.ps1

推荐阅读