Skip to content

Instantly share code, notes, and snippets.

@melastmohican
Created February 1, 2019 23:44
Show Gist options
  • Save melastmohican/5d128350c3d446afcf4e07328e77eea5 to your computer and use it in GitHub Desktop.
Save melastmohican/5d128350c3d446afcf4e07328e77eea5 to your computer and use it in GitHub Desktop.
Select Java folder and set JAVA_HOME environment variable
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }
$paths = (Get-ChildItem "C:\Program Files\Java" -Directory | Select-Object FullName | ForEach-Object { $_.FullName })
$fileChoices = @()
for ($i=0; $i -lt $paths.Count; $i++) {
$fileChoices += [System.Management.Automation.Host.ChoiceDescription]("$($paths[$i]) &$($i)")
}
$userChoice = $host.UI.PromptForChoice('Select File', 'Choose a file', $fileChoices, 0)
$path = $paths[$userChoice]
Write-Host "$path"
# $filename = Get-ChildItem "C:\Program Files\Java" -Directory | Select-Object FullName | Out-GridView -Title 'Choose a directory' -PassThru | ForEach-Object { $_.FullName }
# Write-Host "$filename"
### Set an environment variable using the System.Environment .NET class
[System.Environment]::SetEnvironmentVariable('JAVA_HOME', "$path" ,[System.EnvironmentVariableTarget]::Machine)
Write-Host -Object ('The key that was pressed was: {0}' -f [System.Console]::ReadKey().Key.ToString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment