Sea*_*dre 2 shell powershell batch-file command-prompt windows-console
我的计算机上有一个批处理脚本,名为cs.bat. 当我进入cs命令提示符时,pushd将我带到某个目录并将我留在那里。在 PowerShell 中,该命令执行相同的操作,但会将我带回起始目录。
为什么会这样?在 Power Shell 中键入“cs”后,如何才能使其留在目录中?
Powershell 包括 Pushd 和 Popd 的别名。
Get-Alias Pushd : pushd -> Push-Location
Get-Alias Popd : popd -> Pop-Location
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用Get-Help Push-Location -Full -Online获取该 cmdlet 的最新帮助。
然后只需制作一个脚本并测试此行为。
#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.
Run Code Online (Sandbox Code Playgroud)
发生这种情况是因为您的“cs.bat”在 PowerShell 生成的不同进程(运行 cmd.exe)中运行(而批处理文件在从 运行时在同一实例中执行cmd)。当前目录是每个进程的概念,因此在一个进程中更改它不会对另一个进程产生影响。
解决这个问题的最简单方法可能是编写一个“cs.ps1”脚本(或函数),该脚本将在 PowerShell 进程中运行。