13

I'm trying to go through the Active Directory, and grab users who meet a certain criteria. I want to get users have either Manager A or Manager B, but I'm not sure how to implement the or statement. Here's my code:

Get-ADUser -Filter * -Properties country, extensionattribute9 | if (extensionattribute9 -eq 'Smith, Joe') or (extensionattribute9 -eq 'Doe, John') {select extensionsattribute9, country}

In this code, it doesn't recognize extensionattribute9, which gives you the user's manager.

I also tried attempted using where instead of if, but to no avail.

1 Answer 1

32

The operator is -or, not or. See about_Logical_Operators. Also, if statements don't read from the pipeline. Either put the if statement in a ForEach-Object loop:

... | ForEach-Object {
  if ($_.extensionattribute9 -eq 'Smith, Joe' -or $_.extensionattribute9 -eq 'Doe, John') {
    $_ | select extensionsattribute9, country
  }
}

or use a Where-Object statement instead:

... | Where-Object {
  $_.extensionattribute9 -eq 'Smith, Joe' -or
  $_.extensionattribute9 -eq 'Doe, John'
 } | Select-Object extensionsattribute9, country

And you can't use property names by themselves. Use the current object variable ($_) to access properties of the current object.

For checking if an attribute has one of a given number of values you can also use the -contains operator instead of doing multiple comparisons:

'Smith, Joe', 'Doe, John' -contains $_.extensionattribute9
1
  • -or still gives me the error that extensionattribute9 is not recognized.
    – Luke K
    Jun 23, 2016 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.