Acerbity
Acerbity

Reputation: 527

Powershell -contains operator

I am working to get something working. I am trying to build a script to create user accounts and I want to verify that they don't already exist in the system. Here is my code snippet.

#Check if users exist
$ObjOU = [ADSI]"WinNT://$computer"
$colUsers = ($objComputer.psbase.children | Where-Object {$_.psBase.schemaClassName -eq "User"} | Select-Object -expand Name)
$blnFound = $colUsers -contains $userin -or $userout
if ($blnFound) {"The user exists"}
else {"Creating user account"}

The issue I am having is that no matter what the array contains, the $blnfound variable comes back as true so long as I am using the -or operator. If I test it one at a time, without the -or, both come back as false.

Is there a different way that I should be doing this?

Upvotes: 0

Views: 1088

Answers (1)

Acerbity
Acerbity

Reputation: 527

Red Alert nailed it. Order of operations.

$colUsers -contains $userin -or $colUsers -contains $userout

Upvotes: 1

Related Questions