7

I have a user list of Active Directory that I retrieve this way:

$users = Get-AdUser -Filter {(Enabled -eq "True" )} -Properties Description 

The problem is that I have a specific set of users that is based in their description:

  • Admins
  • Secretaries
  • Mail men

What I do is create sublists like this:

$Admins = $users | Where-Object Description -eq 'Administrator'

The problem however is, that there is no standardization. The person who creates an user can write 'Admin' or 'Administrator' or 'adm',... which causes my sublist not to contain all users that are an admin.

What I did is that I created an array of strings:

$Admin_User_Strings = @("adm", "admin", "administrator")

And I wanted to use this array in my sublist but this appearantly doesn't work:

$Admins = $users | Where-Object $Admin_User_Strings -contains Description 

I get the errror:

Where-Object : A positional parameter cannot be found that accepts argument 'System.Object[]'.

So my question is, how can I let the following line:

$Admins = $users | Where-Object Description -eq 'Administrator'

accept more ways of 'Administrator' inputs?

4
  • 2
    Where-Object {$Admin_User_Strings -contains $_.Description} or Where-Object Description -in $Admin_User_Strings Commented Apr 14, 2017 at 8:54
  • 2
    or Where-Object Description -like adm* Commented Apr 14, 2017 at 8:55
  • No one is stopping you from using " or ' :-) Commented Apr 14, 2017 at 9:57
  • oh okay I wasn't aware of that detail. heel erg bedankt alleszinds Commented Apr 14, 2017 at 10:08

1 Answer 1

21

You have several options:

$users | Where-Object {$Admin_User_Strings -contains $_.Description}

or: $users | Where-Object $_.Description -in $Admin_User_Strings

or: $users | Where-Object $_.Description -match "adm|admin|administrator"

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, the -match-option will help me out a lot. Thanks Mosh

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.