Nini Sidhu
Nini Sidhu

Reputation: 1

PowerShell and match/like/contain operators

I am fairly new to PS so running into this problem. I have 2 variables:

    $var1= "hello"
    $var2= "hello-2018"
    
    if ($var1 -match $var2){
       #Do Something 
    }

I want to compare the two variables and if the $var1 has some parts of $var2, I want the code to do something. I’ve tried match, like, contains but not getting the desired output.

Upvotes: 0

Views: 681

Answers (1)

Kevin Kostrzewa
Kevin Kostrzewa

Reputation: 373

How about

$var1= "hello"
$var2= "hello-2018"
    
if ($var2 -like "*$var1*"){
   #Do Something 
}

Also, I think https://morgantechspace.com/2016/08/powershell-check-if-string-contains-word.html would make a good read on the topic

Upvotes: 1

Related Questions