3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PowerShellでユーザ環境変数を設定する

Posted at

この記事は何?

PowerShell で環境変数を操作するとき、プロセス内限定であれば、PATHを例にすると Get-Item env:PATHSet-Item env:PATH "xxx"$env:PATH += "value" などで環境変数の操作ができる。

一方で、プロセスを終了後に、再度PowerShellを起動したときに有効になるような、マシンの環境変数や、ユーザの環境変数を変更する方法がある。

方法

# なければ追加したいパス
$PathValue = "C:\tools\mdctags\bin"

# 環境変数を得る
$OldPathVar = [System.Environment]::GetEnvironmentVariable("PATH", "User")

# 環境変数に任意のパスを加える
$PathArray = $OldPathVar.Split(";")
if ($PathArray.IndexOf($PathValue) -eq -1) {
    $PathArray += $PathValue
}
$NewPathVar = $PathArray -join ';'
$NewPathVar

# 新しい環境変数を設定する
[System.Environment]::SetEnvironmentVariable("PATH", $NewPathVar, "User")

参考資料

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?