在Windows Power Shell和命令提示符中使用pushd

4
我在电脑上有一个批处理脚本叫做cs.bat。当我在命令提示符中输入cs时,pushd会带我进入某个目录并将我留在那里。在PowerShell中,该命令做了同样的事情,但是却把我带回到起始目录。
为什么会这样呢?如何才能在在PowerShell中输入'cs'后停留在目录中?
内容来源:Windows PowerhShell versus Windows command prompt and batch file edited in Notepad
2个回答

11

Powershell 包含 Pushd 和 Popd 的别名。

Get-Alias Pushd : pushd -> Push-Location
Get-Alias Popd : popd -> Pop-Location

您可以使用Get-Help Push-Location -Full -Online命令获取该命令的最新帮助文档。

然后只需编写脚本并测试此行为即可。

#Sample.ps1 script

#Get current DIR
dir

#push location to some location and DIR there.
Push-Location C:\Scripts\
dir

#at this point, your console will still be in the Push-Location directory
#simply run the Pop-Location cmdlet to switch back.

5
这是因为你的“cs.bat”在PowerShell生成的不同进程(cmd.exe)中运行(当从cmd运行批处理文件时,它们在同一实例中执行)。 当前目录是一个进程级别的概念,因此在一个进程中更改它对另一个进程没有影响。 可能最简单的方法是编写一个“cs.ps1”脚本(或函数),它将在PowerShell进程中运行。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接