PATH

  • In a .ps1  file.

  • I tried adding a ENV variable to the SYSTEM ( "Machine" ), but it only works to the USER ( "User" ); maybe it's due to administrator privileges.

# Set PATH
$entry = "C:\clang64\bin"
$path_type = "User" # "Machine" is for System PATH

$env_path = [Environment]::GetEnvironmentVariable("Path", $path_type)

$exists = $env_path -split ";" | Where-Object {
    $_.TrimEnd("\") -ieq $entry.TrimEnd("\")
}

if (-not $exists) {
    if ([string]::IsNullOrEmpty($env_path)) {
        $new_path = $entry
    } else {
        $new_path = "$env_path;$entry"
    }
    [Environment]::SetEnvironmentVariable("Path", $new_path, $path_type)

    Write-Host "'$entry' added to the $path_type PATH"
} else {
    Write-Host "'$entry' already added to the $path_type PATH"
}

Read-Host "Press enter to exit"