1

So I've been fiddling around with powershell and I almost have something that lets me quickly swap between jdk's but the function requires admin privileges for certain statements. Is there a way I can elevate the privileges of this function and then run the script as usual? Here's the function.

Function javachange 
{
    $path = "C:\Program Files\Java\"

    $versions = Get-ChildItem $path | 
       Where-Object {$_.PSIsContainer} | 
       Foreach-Object {$_.Name}
       
    $result = for ($i = 0; $i -lt $versions.length; $i++) 
    {
        Write-Host(" " + ($i + 1) + ") Version: " + $versions[$i])
    }

$in = Read-host "Pick a version you would like to use"

    if ($versions[($in)])
    {
        Write-Host("Setting to version " + $versions[($in)])
        [System.Environment]::SetEnvironmentVariable("JAVA_HOME",  $path + $versions[$in], 
        [System.EnvironmentVariableTarget]::Machine)
        refreshenv
    }
    else
    {
        "Version doesn't exist. Try again."
    } 
}

0 Answers0