Filtering multiple attributes for users in PS
Filtering multiple attributes for users in PS
This morning we tried to work on filtering multiple attributes of data for ADusers. Some of you were
very close to finding the solution. Here is the answer with slightly detailed explanations.
We still use the get-aduser command with the –filter option, but then the code line becomes more
complex. Since we want to filter two or more filter options, we need to use –and between what we
want to filter for.
If we want to filter on enabled or disabled accounts, we set the first filter option as enabled -eq $true or
enabled –eq $false. This set our first filter. The next filter we wanted to search for are specific users that
are in the first filter group. Our next filter could be samaccountname -like “user*”, which would look for
all the users that have ‘user’ starting their account name.
In order to put these both together we tried using (), and ‘’, and just –and, but we just got errors.
Reason was that the system could not understand what we were try to write. Our concepts were good,
but the syntax was wrong.
Braces, or curly brackets are used whenever you want to store a statement block
within your script. Since we are putting two commands together, filter would be
able to read them in braces and perform both functions because it reads them as a
block. In out block we would put both filter options, but insert the –and between the
two. It would look like: {enabled -eq $true –and samaccountname -like “user*”}.
Our command would be get-aduser –filter {enabled -eq $true –and samaccountname -like “user*”}.
get-aduser –filter {enabled -eq $true –and samaccountname -like “user*”} |table-format
samaccountname,enabled.
NOTE: this is all one line. There is to be a space between table-format and
samaccount,enabled. No spaces between samaccountnamd and enabled.