Skip to content

Instantly share code, notes, and snippets.

@mkht
Created July 18, 2018 13:48
Show Gist options
  • Save mkht/c1ee9e16dc9d434ac5deed925ce84f67 to your computer and use it in GitHub Desktop.
Save mkht/c1ee9e16dc9d434ac5deed925ce84f67 to your computer and use it in GitHub Desktop.
環境変数PATHの末尾に指定の値を追加する
<# ++++++++++++++++++++++++++++++++++++++++++++++
環境変数PATHの末尾に指定の値を追加する
+++++++++++++++++++++++++++++++++++++++++++++++++ #>
function Add-EnvironmentPath {
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true, Position = 0)]
[string] $Path, # 追加する値
[Parameter(Mandatory = $true, Position = 1)]
[ValidateSet("User", "Machine")]
[string]$Target # UserかMachineか選ぶ
)
$PathEnv = New-Object System.Collections.ArrayList
([System.Environment]::GetEnvironmentVariable("Path", $Target)) -split ';' | ForEach-Object {$PathEnv.Add($_)} | Out-Null
if ($PathEnv -notcontains $Path) {
$PathEnv.Add($Path)
[System.Environment]::SetEnvironmentVariable("Path", ($PathEnv -join ';'), $Target)
}
[System.Environment]::GetEnvironmentVariable("Path", $Target)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment