Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

powershell -Or operator not working

Tags:

powershell

Can someone tell me why the -or does not work. If I run the code the first output is administrator which returns true but when it gets to the kahuna account it 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

Here are the accounts listed in order they appear

administrator, DefaultAccount, Guest, Kahuna, PCUser

like image 365
Miguel De Santiago Avatar asked Apr 11 '17 23:04

Miguel De Santiago


1 Answers

Try

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

Your -or operator doesn't tie the values of the previous operator together. You need to specify a 2nd conditional operator after -or I believe.

like image 101
Nick Avatar answered Sep 20 '22 05:09

Nick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!