John W
John W

Reputation: 129

Powershell : set enviroment variable doubled

The code below tries to change the enviroment variable PATH but unfortunately the strings are doubled :

Blockquote 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 Blockquote

the ';C:\LJ\jdk-13\bin' is the only string I wanted to add

Do you know where is the error ? Thanks

    $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)
}    

Upvotes: 2

Views: 211

Answers (1)

John W
John W

Reputation: 129

$PATH = [System.Environment]::GetEnvironmentVariable('PATH', [System.EnvironmentVariableTarget]::User)
    $PATH += ";" + $jdkInstallFolder +"\bin"
    [System.Environment]::SetEnvironmentVariable('PATH',$PATH, [System.EnvironmentVariableTarget]::User)

I have understood, thanks @Lee_Dailey!

Upvotes: 0

Related Questions