Skip to content

Instantly share code, notes, and snippets.

@kentork
Last active July 15, 2019 06:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kentork/bf783d2a5378f32dbacb40d8897e7942 to your computer and use it in GitHub Desktop.
Save kentork/bf783d2a5378f32dbacb40d8897e7942 to your computer and use it in GitHub Desktop.
Environment operation for powershell
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