Appearance
条件分支
PowerShell 条件分支有 if 、switch 等。
判断名称是否为 powershell
$name = 'powershell'
if ($name -eq 'powershell') {
Write-Host '名称是 powershell'
} else {
Write-Host '名称不是 powershell'
}判断名称是否为 powershell
$name = 'powershell'
$message = ($name -eq 'powershell') ? '名称是 powershell' : '名称不是 powershell'
Write-Host $message判断用户是否为 Administrator
if ($env:USERNAME -eq 'Administrator') {
Write-Host '用户名是 Administrator'
} else {
Write-Host '用户名不是 Administrator'
}判断数字
switch (2) {
1 {
Write-Host '数字是 1'
}
2 {
Write-Host '数字是 2'
}
default {
Write-Host 'default'
}
}