LoginSignup
0
1

More than 3 years have passed since last update.

Powershellで環境変数を設定する

Posted at

Powershellで環境変数を設定する

Powershellでは環境変数は、ドライブに保存されている。
ドライブといえば、C:、E:のファイルシステムのドライブを想像してしまうが、Powershellではデータの保管場所という意味でドライブという言葉が使われている。

環境変数は、env という名前のドライブが定義されている

ドライブという呼び名の通り、CDでそこへ移動したり、 Get-ChildItemで子要素をリストしたりもできる。
ほ~って感じですね。

一時的に設定する場合

一時的に環境変数を変更したい場合は、$envに対して値を設定してやればOK

$env:SomeValue = "This is a new value"

恒久的に環境変数を変更したい場合

恒久的に環境変数を変更してやりたい場合は、.NETのオブジェクトを通して取得・変更ができる

[System.Environment]::GetEnvironmentVariable('PATH','machine')

[System.Environment]::SetEnvironmentVariable('FOO', 'bar',[System.EnvironmentVariableTarget]::Machine)

蛇足

どんなドライブが存在しているかは、コマンドレット Get-PSDriveで取得することができる
Providerの欄を見ると、どのタイプのドライブなのか判断ができる。

Name           Used (GB)     Free (GB) Provider      Root                                               CurrentLocation
----           ---------     --------- --------      ----                                               ---------------
Alias                                  Alias
C                 392.84         82.31 FileSystem    C:\                                                   Users\hogehoge
Cert                                   Certificate   \
D                                      FileSystem    D:\
E                2433.71        360.68 FileSystem    E:\
Env                                    Environment
F                2141.91        652.49 FileSystem    F:\
Function                               Function
G                 407.53         58.23 FileSystem    G:\
H                2733.60         60.79 FileSystem    H:\
HKCU                                   Registry      HKEY_CURRENT_USER
HKLM                                   Registry      HKEY_LOCAL_MACHINE
I                1960.81        833.58 FileSystem    I:\
Variable                               Variable

FileSystemやEnvironmentの他に、Certificate、Function、Registryなんてのもあり、
Providerにデータベースを追加することもできるらしい。

参考

静的なクラスとメソッドの使用

0
1
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
1