PowerShellで変数宣言をする方法をいくつかご紹介します。
構文
$変数名 = 値
文字列の場合
$hoge = “aaaaa”
参照方法
$hoge
数字の場合
$hoge2 = 12345
参照方法と型は文字列と同様
型を宣言
[型式]$変数名 =
変数宣言時、変数の型は自動で選択されますが、明示的に宣言する方法もあります。
New-Variableコマンドレット
New-Variable 変数名 -value 値
New-Variable hoge5 -value “bbbbb”
不要となった変数の削除
PowerShellのコンソールで一度変数を作成してしまうと、PowerShellを終了するまでその変数は残ってしまいます。
Remove-Variableコマンドレットを使用すると、任意のタイミングで指定した変数とその値を削除することができます。
上記画像のように一度削除した変数を、再度削除しようとするとエラーが発生します。
変数の型一覧
byte System.Byte
int System.Int32
long System.Int64
single、float System.Single
double System.Double
decimal System.Decimal
char System.Char
bool System.Boolean
string System.String
array System.Array
xml System.Xml.XmlDocument
type System.Type
powershell記事一覧