Set-Variable, Get-Variable
$ を使って変数の読み書きをする以外に、 Set-Variable と Get-Variable という Cmdlet を使うことでも変数の読み書きができます。
> Set-Variable a 1
> Get-Variable a
Name Value
---- -----
a 1
> $a = 2
> Get-Variable a
Name Value
---- -----
a 2
まあ、単に値を代入するだけなら Set-Variable は必要ないんですが、 Set-Variable を使うと、 ReadOnly / Constant 属性を付与することができます。
> Set-Variable a 1 -option ReadOnly
> $a = 0
変数 a は読み取り専用または定数であるため、上書きできません。
また、 Get-Variable では任意のレベルのスコープの変数にアクセスしたりできます。
> Get-Variable a –scope 1 # 親スコープから値を取得
> $Get-Variable a –scope 2 # 祖父
その他、Remove-Variable で変数を削除したりもできます。
> $a = 0
> Remove-Variable a
> Get-Variable a
Get-Variable : 名前 'a' の変数が見つかりません。
