4

Found this while debugging a Powershell script.

PS C:\temp> "hello" -match $null  
True  

But

> "hello" -eq $null returns False

Why, when using -match, any string will match with $null?

1
  • 1
    Presumably because the $null regular expression matches anything because it matches against nothing. Similarly you can insert as many ${null}s as you want into the middle of a pattern and not affect its matching because they simply expand to nothing. Commented Apr 14, 2015 at 18:11

1 Answer 1

6

I believe this is because the $null gets coerced to an empty string as -match expects a string pattern on the RHS (Right Hand Side). And an empty string will match anything.

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

2 Comments

Thanks. Yeah checked and -match with any empty/null variable is True. Empty strings match anything, learn something new everyday :-)
This must be correct, for "hello" -match [NullString]::Value explodes with an ArgumentNullException.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.