0
0

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 1 year has passed since last update.

PowerShell でシステム環境変数を設定・削除してみた

Posted at

前提

  • PowerShell は管理者モードで起動していることとする。

1. バージョン情報

1.1. Windows OS Version

PS C:\Users\Administrator> (Get-CimInstance -ClassName Win32_OperatingSystem).Version
10.0.19045
PS C:\Users\Administrator>

1.2. PowerShell Version

PS C:\Users\Administrator> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      5.1.19041.3031
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.3031
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
PS C:\Users\Administrator>

2. 事前確認

  • 何も表示されないのが正。
PS C:\Users\Administrator> $variableName = "hoge"
PS C:\Users\Administrator> [System.Environment]::GetEnvironmentVariable($variableName, [System.EnvironmentVariableTarget]::Machine)
PS C:\Users\Administrator>

3. システム環境変数の設定

3.1. 設定

PS C:\Users\Administrator> $variableName = "hoge"
PS C:\Users\Administrator> $variableValue = "hogehoge"
PS C:\Users\Administrator> [System.Environment]::SetEnvironmentVariable($variableName, $variableValue, [System.EnvironmentVariableTarget]::Machine)
PS C:\Users\Administrator>

3.2. 確認

PS C:\Users\Administrator> [System.Environment]::GetEnvironmentVariable($variableName, [System.EnvironmentVariableTarget]::Machine)
hogehoge
PS C:\Users\Administrator>

4. システム環境変数の削除

4.1. 削除

PS C:\Users\Administrator> $variableName = "hoge"
PS C:\Users\Administrator> $variableValue = $null
PS C:\Users\Administrator> [System.Environment]::SetEnvironmentVariable($variableName, $variableValue, [System.EnvironmentVariableTarget]::Machine)
PS C:\Users\Administrator>

4.2. 確認

  • 何も表示されないのが正。
PS C:\Users\Administrator> $variableName = "hoge"
PS C:\Users\Administrator> [System.Environment]::GetEnvironmentVariable($variableName, [System.EnvironmentVariableTarget]::Machine)
PS C:\Users\Administrator>
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?