Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How to put in multiple values in a where-object filter

How to put in multiple values in a where-object filter

How to put in multiple values in a where-object filter

I have some code:

Get-ADGroupmember -Identity Servers|Select-Object -ExpandProperty name|Sort-Object

Which returns a simple list of members of the "Servers" group.

A foreach loop then does other stuff with that list (finds stopped services and restarts them)

What I would like to do is to give myself the option of sometimes excluding some servers from that list.

I know I could put in a where-object{$_.name -ne 'server1' -and....... } etc, but the list changes each time and I would like to automate this with a scheduled task so to make it easier I wonder if I could use some kind of variable. something along the lines of:

Get-ADGroupmember -Identity Servers|Where-Object{$_.name -ne $excludedservers}|Select-Object -ExpandProperty name|Sort-Object

This doesn't work though, it still produces the full list.

Can anyone point me in the right direction? maybe a foreach is needed? I cant see how to do it.


RE: How to put in multiple values in a where-object filter

Slight modification to your filter script block on the Where-Object. You are looking to compare a single value ($_.name) to ensure it is not in the array of excluded servers

Get-ADGroupmember -Identity Servers | 
    Where-Object{$_.name -notin $excludedservers} |
    Select-Object -ExpandProperty name |

Make your code readable and put line breaks in where there is a logical separator in the PowerShell statement. If you end a line at the pipe character '|' PowerShell will know that a pipeline exists and there is more code to follow.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close