Kahn Kah
Kahn Kah

Reputation: 1453

Use array of strings for comparing in Where-Object in PowerShell

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:

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?

Upvotes: 7

Views: 35562

Answers (1)

Moshe perez
Moshe perez

Reputation: 1726

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"

Upvotes: 21

Related Questions