创建变量及赋值
https://blog.netwrix.com/2018/10/04/powershell-variables-and-arrays/
$var_str1 = "Hello";
$var_str2 = "World";
$var_str3 = $var_str1 + $var_str2
$var_str3 # HelloWorld
$var_int1 = 10;
$var_int2 = 20;
$var_int3 = $var_int1 + $var_int2
$var_int3 # 30
$var_type_conv = $var_str1 + $var_int1
$var_type_conv # Hello10
$str_to_int = [int]"123"
$var_array = "apple", "orange", 123
$var_array.GetType().FullName # 属性和命令均不区分大小写
$var_array2 = @(elem1, elem2)
$var_hashtable = @{
"key1" = "value1"; # 能用分号或者直接留空,但不能加逗号
"key2" = 123
}
# 作用域
# Global: 在当前powershell交互环境(会话)里一直有效
# Script: 整个psl脚本
# Local: 当前作用域(块)
$global:var
$script:var
$local:var
# 清空变量,或置空
Clear-Variable -Name var_str1 # 现在var_str1值为$null
# 销毁变量
Remove-Variable $var_str1 # 现在var_str1变量不复存在,访问会报错
Set-Variable -Name "myVariable" -Description "This is my sample variable"