4
7

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でシステム環境変数を取得・上書・追加・削除する

Last updated at Posted at 2020-09-26

#環境変数の一覧取得

PowerShell
PS C:> [Environment]::GetEnvironmentVariables("Machine") # システム環境変数一覧
PS C:> [Environment]::GetEnvironmentVariables("User")    # ユーザー環境変数一覧
PS C:> [Environment]::GetEnvironmentVariables("Process") # プロセス環境変数一覧

#環境変数の取得

PowerShell
PS C:> [Environment]::GetEnvironmentVariable("Path", "Machine")

プロセス環境変数の取得は $env:path でも可能。

#環境変数の設定(新規・上書き・削除)

PowerShell
PS C:> [Environment]::SetEnvironmentVariable("Path", "C:\bin", [EnvironmentVariableTarget]::Machine)

第2引数を空文字 "" にすることで、環境変数を削除できる。(環境変数のキーと値の両方が削除される)
※システム環境変数の変更は、管理者権限のみ可能。ユーザー環境変数とプロセス環境変数は一般ユーザーでも変更可能。

#環境変数を追加

PowerShell
PS C:> [Environment]::SetEnvironmentVariable("Path", [Environment]::GetEnvironmentVariable("Path", "Machine")+";C:\bin", [EnvironmentVariableTarget]::Machine)

#参考文献
環境変数の操作 - env

4
7
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
4
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?