我的计算机上有一个名为cs.bat
的批处理脚本。当我在命令提示符中输入cs
时,pushd
将我带到某个目录并离开我。在PowerShell中,该命令执行相同的操作,但随后将我带回到起始目录。
为什么会这样?我怎样才能这样做,以便在输入“cs'”之后留在目录中进入电源壳?
答案 0 :(得分:4)
这种情况正在发生,因为您的“cs.bat”在PowerShell生成的不同进程(运行cmd.exe)中运行(而批处理文件在从cmd
运行时在同一实例中执行)。当前目录是每个进程的概念,因此在一个进程中更改它对另一个进程没有影响。
可能最简单的解决方法是编写一个“cs.ps1”脚本(或函数),该脚本将在PowerShell进程中运行。
答案 1 :(得分:2)
Powershell包含Pushd和Popd的别名。
Get-Alias Pushd : pushd -> Push-Location
Get-Alias Popd : popd -> Pop-Location
然后,您可以使用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.