powershell无法调用Path中Program Files (x86)下的程序

在Path环境变量加入一个在Program Files (x86)下的路径后,Get-Command可以正常直接获取该路径下的程序,但执行时会因为路径里的“x86”而报错。如下所示:

C:\> (Get-Command code).Source
C:\Program Files (x86)\Microsoft VS Code\bin\code.cmd
C:\> code
警告: Could not find ssh-agent
x86 : 无法将“x86”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果
包括路径,请确保路径正确,然后再试一次。
所在位置 行:1 字符: 19
+ C:\Program Files (x86)\Microsoft VS Code\bin\code.cmd
+                   ~~~
    + CategoryInfo          : ObjectNotFound: (x86:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

C:\> $env:PATH
C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program File
s\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\Syste
m32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Component
s\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)
\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management E
ngine Components\IPT;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\
Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\WINDOWS\system32;C:\WI
NDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\n
odejs\;C:\Program Files (x86)\scala\bin;C:\Program Files\TortoiseGit\bin;C:\WINDOWS\system32\
config\systemprofile\.dnx\bin;C:\Program Files\Microsoft DNX\Dnvm\;C:\Program Files (x86)\Win
dows Kits\8.1\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\120\Tools\Bi
nn\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files\Git\cmd;C:\Users\D
ustar\AppData\Local\Microsoft\WindowsApps;C:\Users\Dustar\AppData\Roaming\npm;C:\Program File
s (x86)\Microsoft VS Code\bin
C:\>

这个问题在cmd里并不会出现。
请问这是什么原因,该怎么解决?

阅读 11k
3 个回答

后来找到原因是把环境变量里的ComSpec改成powershell就会出这个问题

新手上路,请多包涵

cd 'C:Program Files (x86)' 只需要对路径加 '' 就好了

看样子应该是路径中有空格,导致解析时出错,你可以把路径用引号包起来,避免空格造成歧义。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进