如何使用 PowerShell 更改 cmd 的当前目录? [英] How to change the cmd's current-dir using PowerShell?

查看:42
本文介绍了如何使用 PowerShell 更改 cmd 的当前目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 PowerShell 读取了一些文件,并相应地更改了当前目录,但我所能做的只是更改当前 PowerShell 的当前目录,而不是调用者的目录(调用该 ps1 文件的 cmd.exe 环境).我尝试过的事情:

I read some file using PowerShell, and change current dir accordingly, but all I can do is change the current PowerShell's current dir, not the caller's dir (the cmd.exe environment that called that ps1 file). Things I tried:

powershell ch-dir.ps1 | cd

(显然,这是行不通的,因为 CD 是内部命令)

(won't work, obviously, since CD is internal command)

powershell cd $myDir

(更改PowerShell中的当前目录,但当脚本退出时,cmd环境仍在原始目录中)

(changes current dir in PowerShell, but when script exits, the cmd environment still in original dir)

我真的希望我不需要找到脚本的调用者进程(cmd),并强制更改它的 cur-dir...(或者更糟 - 将我想要的目录保存在一些env-var 然后是 cd %my_var% 因为它需要两行命令)

I really hope I won't need to find the script's caller process (the cmd), and make a change in it's cur-dir by-force... (or even worse - to save the dir I want in some env-var and then cd %my_var% since it would require two lines of command)

推荐答案

我不确定这是否满足您的需求,但如果您设置它以便您的 powershell 脚本的唯一输出是您想要的新工作目录,你可以这样做:

I'm not sure if this meets your needs, but if you set it up so that the only output from your powershell script is your desired new working directory, you could do this:

c:>for /F %i IN ('powershell -noprofile -command "write-output 'c:users'" ') DO @cd %i
c:Users>

这篇关于如何使用 PowerShell 更改 cmd 的当前目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆