0

I have a test branch, I want to test its existence with -contains, but it doesn't seem to work ?

$test = git branch if ($test -contains "test") { Write-Host "success" } 
2
  • what's the output of git branch? the -contains operator is quite literal when it comes to its name, just means"test" is being searched in an array. Commented Mar 11, 2022 at 23:12
  • 2
    -contains / -notcontains are collection operators: they test if the LHS object is equal in full to at least one element of the RHS collection. They are not to be confused with the .Contains() .NET method for substring matching. While PowerShell has no equivalent operator for literal substring matching, you can use -like with wildcard expressions or -match with regular expressions, both of which are case-insensitive by default. Commented Mar 11, 2022 at 23:13

1 Answer 1

1

The keyword you're looking for is -like

https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-if?view=powershell-7.2#-like-wildcard-matches

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.