Powershell -Or not working

Can someone tell me why -or or not working. If I run the code, the first exit is an administrator who returns true, but when he gets to the kahuna account, he still returns false, not true.

(Get-LocalUser).Name | Out-File C:\localusers.txt

ForEach ($User in Get-Content C:\localusers.txt)
{
    If ($User -match "administrator" -or "kahuna")
    {
        Write-Host True
    }
    Else
    {
        Write-Host False
    }
}

I get

True, False, False, False, False

The following are the accounts displayed in the order they appear.

Admin, DefaultAccount, Guest, Kahuna, PCUser

+6
source share
2 answers

Try

If ($User -match "administrator" -or $User -match "kahuna")

Your statement -ordoes not bind the values ​​of the previous statement together. You need to specify the second conditional statement after -orI suppose.

+8
source

, . , :

 If (($User -match "administrator") -or ($User -match "kahuna"))

, PSH . $User = "administrator" :

If (($true) -or ($false))

$true.

+2

Source: https://habr.com/ru/post/1674614/


All Articles