Last active
July 15, 2019 06:46
-
-
Save kentork/bf783d2a5378f32dbacb40d8897e7942 to your computer and use it in GitHub Desktop.
Environment operation for powershell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function setenv($key, $value, $target) { | |
if (! $target) { | |
$target = "User" | |
} | |
if (($target -eq "Process") -Or ($target -eq "User") -Or ($target -eq "Machine")) { | |
$now = [environment]::getEnvironmentVariable($key, $target) | |
if ($now) { | |
$tChoiceDescription = "System.Management.Automation.Host.ChoiceDescription" | |
$result = $host.ui.PromptForChoice("", "Already Exists. Overwrite ?", @( | |
New-Object $tChoiceDescription ("&Yes") | |
New-Object $tChoiceDescription ("&No") | |
), 1) | |
switch ($result) { | |
0 {break} | |
1 { | |
Write-Host "`r`nAborted." -ForegroundColor DarkRed | |
return | |
} | |
} | |
} | |
[environment]::setEnvironmentVariable($key, $value, $target) | |
[environment]::setEnvironmentVariable($key, $value, "Process") | |
} else { | |
Write-Host "Failure ! - Invalid Target" -ForegroundColor DarkYellow | |
} | |
} | |
function setpath($value, $target) { | |
if (! $target) { | |
$target = "User" | |
} | |
if (($target -eq "Process") -Or ($target -eq "User") -Or ($target -eq "Machine")) { | |
$item = Convert-Path $value | |
$path = [environment]::getEnvironmentVariable("PATH", $target) | |
$list = $path -split ";" | |
if (! $list.Contains($item)) { | |
$_path = $path + ";" + $item + ";" | |
$newpath = $_path -replace ";;", ";" | |
[environment]::setEnvironmentVariable("PATH", $newpath, $target) | |
$_path = [environment]::getEnvironmentVariable("PATH", "Process") + ";" + $item + ";" | |
$newpath = $_path -replace ";;", ";" | |
[environment]::setEnvironmentVariable("PATH", $newpath, "Process") | |
} else { | |
Write-Host "Already Exists." -ForegroundColor DarkYellow | |
} | |
} else { | |
Write-Host "Failure ! - Invalid Target" -ForegroundColor DarkRed | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment