Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Windows Power Shell和命令提示符中按下_Shell_Powershell_Batch File_Command Prompt_Windows Console - Fatal编程技术网

在Windows Power Shell和命令提示符中按下

在Windows Power Shell和命令提示符中按下,shell,powershell,batch-file,command-prompt,windows-console,Shell,Powershell,Batch File,Command Prompt,Windows Console,我的计算机上有一个批处理脚本,名为cs.bat。当我在命令提示符下输入cs时,pushd将我带到某个目录并将我留在那里。在PowerShell中,该命令执行相同的操作,但随后将我带回起始目录 为什么会这样?在powershell中键入“cs”后,如何使其留在目录中 发生这种情况是因为“cs.bat”在PowerShell生成的不同进程(运行cmd.exe)中运行(而批处理文件在从cmd运行时在同一实例中执行)。当前目录是每个进程的概念,因此在一个进程中更改它对另一个进程没有影响 可能最简单的解决

我的计算机上有一个批处理脚本,名为
cs.bat
。当我在命令提示符下输入
cs
时,
pushd
将我带到某个目录并将我留在那里。在PowerShell中,该命令执行相同的操作,但随后将我带回起始目录

为什么会这样?在powershell中键入“cs”后,如何使其留在目录中


发生这种情况是因为“cs.bat”在PowerShell生成的不同进程(运行cmd.exe)中运行(而批处理文件在从
cmd
运行时在同一实例中执行)。当前目录是每个进程的概念,因此在一个进程中更改它对另一个进程没有影响


可能最简单的解决方法是编写一个“cs.ps1”脚本(或函数),该脚本将在PowerShell进程中运行。

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.