常用集合,方便后续复制粘贴
# 判断文件在不在
# 输出文件
IF(!(test-path $filePath))
{$result|Out-File $filePath
}# 读取txt
$result=(Get-Content $filePath -TotalCount 1).Trim()
$result# 删除文件
remove-item "C:\wistron\Datasource\spiderPort.txt"
Test-Path,检查路径是否存在。
语法:Test-Path <路径>
说明:这里的路径可以是:文件、文件夹、HKLM路径、环境变量env:路径
Test-Path D:\q.txt
Test-Path C:\Scripts
Test-Path C:\Scripts\Archive\*.ps1
字符串Join命令
"$TargetPath", "$copyFileName" -Join "\"
-Join("$portCount","$ACPIStatus","$VolLetter") //直接拼接
字符串split命令
("$userInput" -split " ").count //空格分组,然后计算个数
("$userInput" -split " ")[$num] $a="abc:def:ghi"
$a -Split ":"
$path.Split(".")[-1] Split-Path -Parent "sourceFullPath" //获取所在文件夹
修改文件名
rename-Item 'D:\For PS\A.txt' -NewName 'aa.txt'
replace
$itemCommonName=$testItem.Replace("`\","-")
$itemCommonName=$testItem.Replace("`/","-")
null语句
PS C:\Users\> $a # 当$a不赋予任何值时
PS C:\Users> $a -eq ""
False
PS C:\Users> $a -eq $null
True
PS C:\Users> $a = "" # 当$a设为空字符串时
PS C:\Users> $a -eq ""
True
PS C:\Users> $a -eq $null
False
可以看到,当$a不赋予任何值是,结果为Null,在powershell 中表示为$null
if语句
$a="S0"
if($acpiCount -eq 1)
{$a="S3"
}
elseif($acpiCount -eq 2)
{$a="S4"
}
If(!(test-path $TargetPath))
{New-Item -ItemType Directory -Force -Path $TargetPath
}$filePath ="$TargetPath", "$copyFileName" -Join "\"
$filePath|Out-File C:\bat\Output_filePath.txtIF(!(test-path $filePath))
{Copy-Item $sourceFullPath -destination $filePath
}
Powershell 中的比较运算符
-eq :等于
-ne :不等于
-gt :大于
-ge :大于等于
-lt :小于
-le :小于等于
-contains :包含
-notcontains :不包含布尔运算-and :和
-or :或
-xor :异或
-not :逆