0

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.

2
  • 1
    you have your match reversed. [grin] to test for A in B, you need to have A on the left ... not on the right. Commented Apr 4, 2022 at 21:13
  • ... or using string methods: if ($var2.Contains($var1)) { } Commented Apr 4, 2022 at 21:39

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.