IF(Test-Path $ Path)-一次检测2个文件?

时间:2019-05-24 17:26:20

标签: powershell

我正在使用下面的代码来测试文件是否存在,但是我需要它来验证2个文件。有没有一种方法可以使用下面的代码检测2个文件的存在?这样的东西行吗?

If (Test-Path $appPath) AND (Test-Path $appPath2)

原文:

Function CurrentUser{
$loggedInUserName = get-wmiobject win32_computersystem | select username
$loggedInUserName = [string]$loggedInUserName
$loggedinUsername = $loggedInUserName.Split("=")
$loggedInUserName = $loggedInUserName[1]
$loggedInUserName = $loggedInUserName.Split("}")
$loggedInUserName = $loggedInUserName[0]
$loggedInUserName = $loggedInUserName.Split("\")
$loggedInUserName = $loggedInUserName[1]
Return $loggedInUserName
}
$user = CurrentUser

$appPath = "C:\Users\" + $user + "\AppData\LocalLow\Test.bat"
If (Test-Path $appPath) {
    Write-Host "User Appdata detected successfully!"
}

1 个答案:

答案 0 :(得分:1)

它将是if (Test-Path $appPath1 -and Test-Path $appPath2) {...,而不是...AND...,但是,PowerShell确实支持复合条件。我也倾向于给各个条件加上括号(if ((Test-Path $appPath1) -and (Test-Path $appPath2)) {...),但这是个人特质。

标准布尔运算符为-and-or-xor-not;有关详细信息,请参见Get-Help about_Logical_Operators -full