Question

Ok I am trying to do something like the following:

Simplified Example:

$Var = "MyProduct*MyProduct"
$List += "MyProduct 11 MyProduct"
$List += "YourProduct 11 YourProduct"
$List += "SomethingElse"
$NewVar = $List | Where {$_ -like "$Var"}

I want the "*" in $Var to be expanded then check if it is -like "$_" so that it will recognize there is a wildcard in the var and get results based on that.. Is there any way to do that?

Was it helpful?

Solution

$list is not an array, each time you add to it you are actually concatenating to it. You can start with a single item array, using the unary comma operator (this is just one way to do it), now each addition will add a new array item:

PS> $List = ,"MyProduct 11 MyProduct"
PS> $List += "YourProduct 11 YourProduct"
PS> $List += "SomethingElse"
PS> $list
MyProduct 11 MyProduct
YourProduct 11 YourProduct
SomethingElse

PS> $List -like $var
MyProduct 11 MyProduct
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top