A String, Arrays, | and a Where-Object

I’m getting a String (cant change the type only content) and would like to filter the output of Get-Cluster based on these values

$filter = “'one”,“two”

The length of the string can be anything between one or unlimited

$AllClusters=get-cluster -Domain $domain

What I need help with is the filter condition based on the input.

Welcome back to the forum. :wave:t3: … long time no see.

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

Could you please elaborate a little more detailed? How does $filter relate to the Get-Cluster command? … and where is your Where-Object? :thinking:

The basic idea I have - but not working is:

$filter = “'one”,“two”
$cluster_filter_array= $cluster_filter.Split(",")

$filterConditions = @()

foreach ($filter in $cluster_filter_array) {

    $filterConditions +=  { $_.Name -like $filter } 


$Clusters = @()

foreach ($condition in $filterConditions) {
    $Clusters += $AllClusters | Where-Object -FilterScript $condition

Now with formatting :smile:


It might be better when you explain what your idea is instead of showing confusing code. :smirk:

  1. Where do you define $cluster_filter?
  2. If you define a proper array with $filter = “'one”,“two” you don’t have to split it. :man_shrugging:t3:

That’s usually unnecessary.

  1. If you use a foreach() loop you define a loop variable you name yourself. You don’t have the default pipeline variable $_ in this case.
  2. You use the same name for your loop variable like you did before for your array.

What do you expect from this line of code? If you run a comparison you get a $true or a $false. Is it really that what you want to collect in an array?

Where do you populate $AllClusters?

Please keep in mind that we cannot see your screen and we cannot read your mind. Take a step back and a deep breath and try to explain in simple terms what you want to achieve. :wink: :love_you_gesture:t3: