在Visual Studio Code中启动集成终端时如何添加到PATH?

时间:2019-01-14 20:44:47

标签: powershell visual-studio-code

从Visual Studio Code运行PowerShell时,我需要在PATH环境变量中附加一个文件夹。仅当从Visual Studio中运行PowerShell时,此更改才应该存在,因为我不希望这些额外的实用程序不可用(即,不能在Windows设置中更新PATH变量。)

我尝试了一些修改用户设置的尝试,但是似乎都没有用。

尝试1

    "terminal.integrated.shell.windows" : "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
    "terminal.integrated.shellArgs.windows" : [
        "-NoExit",
        "-Command \"& {$Env:Path += ';C:\\tools'}\""
    ]

结果:PowerShell出现以下错误。

-Command : The term '-Command' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that
the path is correct and try again.
At line:1 char:1
+ -Command "& {$Env:Path += 'C:\tools'}"
+ ~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-Command:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

尝试2

    "terminal.integrated.shell.windows" : "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
    "terminal.integrated.shellArgs.windows" : [
        "-NoExit",
        "-Command", 
        "\"& {$Env:Path += 'C:\\tools'}\""
    ]

结果:PowerShell从以下输出开始,但是“ C:\ tools”目录未附加到PATH env变量。

& {C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;... += 'C:\tools'}

有人知道如何用VisualStudio Code中的扩展PATH变量调用PowerShell吗?

1 个答案:

答案 0 :(得分:1)

我对VSCode并不是特别熟悉,但是基于错误,Attempt 1似乎已关闭。我认为以下方法会起作用:

    "terminal.integrated.shell.windows" : "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
    "terminal.integrated.shellArgs.windows" : [
    "-NoExit",
    "$Env:Path += ';C:\\tools'"
]