Powershell wildcard character pattern issue

Viewed 49

I am getting the error The specified wildcard character pattern is not valid:

Here is the example.

$trimItem = "What is your house and land budget?"
$title = "(Select)</p> <p>What is your house and land budget? </p> <p>Minimum :  [MinBudget][MaxBudget"

if ($trimItem -clike "*$($title)*") {
    write-host true
}
else {
    write-host false
}

How can I correct this?.

Any suggestions would be appreciated.

Thanks in advance

1 Answers

You're getting a WildcardPatternException due to a missing closing ], note that [ and ] are special Wildcard characters. If you want to match these characters literally they need to be escaped, in PowerShell, the escape character is the backtick `:

[WildcardPattern]::Escape('[ ]') # Returns => `[ `]

However, taking a step back, seems like the order of your condition is incorrect, most likely you wanted to reverse the order of the variables for your comparison:

$title -clike "*$trimItem*" # Returns => True
Related