Powershell:设置环境变量

Powershell:设置环境变量,powershell,variables,path,Powershell,Variables,Path,下面的代码试图更改环境变量路径,但不幸的是字符串加倍: 大宗报价 C:\Windows;C:\Windows\system32;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\; C:\Windows;C:\Windows\system32;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowe

下面的代码试图更改环境变量路径,但不幸的是字符串加倍:

大宗报价 C:\Windows;C:\Windows\system32;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\; C:\Windows;C:\Windows\system32;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\; C:\Users\Arthur\AppData\Local\Programs\Microsoft VS Code\bin;;C:\LJ\jdk-13\bin 大宗报价

",;C:\LJ\jdk-13\bin'是我想要添加的唯一字符串

你知道错误在哪里吗? 谢谢


我明白了,谢谢@Lee_Dailey

$env:path
获得的路径适用于所有目标-机器+用户+进程。您只想更改其中一个,因此使用与您的
Set
调用相当的
Get
。[grin]谢谢,我正在尝试使用$p=[System.Environment]::GetEnvironmentVariable(“Path”)我有两个帐户,可能是这导致了双字符串?不,双字符串是由加载来自
机器
用户
进程
的所有路径引起的。。。然后把它们都放在一个目标上。想一想-如果
ThisThing
位于
user
中,并且您将
OtherThing
添加到整个路径中。。。然后将其保存到
机器
。。。您将有两份这件事的副本。第一个将来自
用户
部分,但第二个将来自
机器
部分。谢谢,但我不知道你的回答有多高。非常欢迎!不需要再投票了。。。我很高兴能帮上忙。[咧嘴笑]
    $zip =  [io.compression.zipfile]::OpenRead($jdkDownloadPathName).Entries
    $jdkFolder = (($zip | Where-Object FullName -match '/' | Select-Object -First 1).Fullname -Split '/')[0]
    $jdkInstallFolder ="$($installJavaAntPath)\$($jdkFolder)"
    $PATH = $env:Path
    $PATH += ";" + $jdkInstallFolder +"\bin"
    Set-EnvironmentVariable -name PATH -Value $PATH -Target User 
    Set-EnvironmentVariable -name JAVA_HOME -Value $jdkInstallFolder -Target User
    function Set-EnvironmentVariable
{
  param
  (
    [Parameter(Mandatory=$true)]
    [String]
    $Name,

    [Parameter(Mandatory=$true)]
    [String]
    $Value,

    [Parameter(Mandatory=$true)]
    [EnvironmentVariableTarget]
    $Target
  )
  [System.Environment]::SetEnvironmentVariable($Name, $Value, $Target)
}    
$PATH = [System.Environment]::GetEnvironmentVariable('PATH', [System.EnvironmentVariableTarget]::User)
    $PATH += ";" + $jdkInstallFolder +"\bin"
    [System.Environment]::SetEnvironmentVariable('PATH',$PATH, [System.EnvironmentVariableTarget]::User)