对变量的操作
1.1 变量操作
1.1.1 定义变量- $x方式(推荐)
$a = 1
$str = "hello world"
1.1.2 定义变量- Set-Variable
Set-Variable a 1
Set-Variable str "hello world"
1.1.3 查看变量
# (查看所有变量)
Get-Variable
# 查看变量a
Get-Variable a
# 查看变量str
Get-Variable str
# 查看变量是以s开头的,不区分大小写
Get-Variable s*
1.1.4 清空变量
# 清空变量a
Clear-Variable a
# 清空多个变量,与查看变量一样的操作
Clear-Variable str*
这只是对变量的值进行清空,变量实际是还存在的
1.1.5 删除变量
# 删除变量a
Remove-Variable a
# 删除多个变量,与前面清空、查询是一样的操作
Remove-Variable str*
1.1.6 计算变量
四则运算、赋值运算、比较运算、逻辑运算都满足
$interget1=1
$interget2=2
# 加、减、乘、除、模
$interget3=$interget1(+|-|*|/|% =|+=|-=|*=|/=|%= !|not|and|or)$interget2
# 字符串操作
$str="hello"
$str1="world"
# 输出,查看结果
Write-Host ($str+$str1)
1.1.7 常量
Set-Variable -Name conpany -Value "重庆城银科技" -Option Constant
# 尝试改变变量值,其实这里会异常的
$conpany="重庆中联信息"
注意:常量不能使用$的方式进行声明,只能使用Set-Variable定义。
1.1.7 变量不同类型自带API的使用
$date=Get-Date
# 打印日期部分
Write-Host $date.Date
# 变量增加一天
Write-Host ($date.AddDays(1))
# 字符串截取
$str.Substring(0,5)
基本上常见的一些操作在Powershell中都支持
原文链接:https://blog.csdn.net/xhl_james/article/details/113697745
联系客服