Skip to content

Instantly share code, notes, and snippets.

@mkht
Created July 18, 2018 13:49
Show Gist options
  • Save mkht/acaca8802c84812cec48bf1e226c9738 to your computer and use it in GitHub Desktop.
Save mkht/acaca8802c84812cec48bf1e226c9738 to your computer and use it in GitHub Desktop.
環境変数PATHから指定の値を削除する
<# ++++++++++++++++++++++++++++++++++++++++++++++
環境変数PATHから指定の値を削除する
+++++++++++++++++++++++++++++++++++++++++++++++++ #>
function Remove-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 -contains $Path) {
$PathEnv = ($PathEnv -ne $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