0

I've been experiencing this weird issue where equality operators will not work intuitively inside a function.

When I call the script copied below in my terminal, the function returns "False", but I am expecting it to return "True". The equivalent inline if(-eq)/else expression works fine, but does not work when embedded inside of a function. Is anyone able to enlighten me as to why this is happening?

Note: I am using PS 5.1.22621.1778

function MatchCheck($input_text_1, $input_text_2)
{
    if ($input_text_1 -eq $input_text_2)
    {
        return($true)
    }

    else
    {
        return($false)
    }
}

MatchCheck("test", "test")
dustmole
  • 1
  • 3
  • You should call the function like this: `MatchCheck "test" "test"` – Olaf Jul 12 '23 at 16:24
  • 2
    ```MatchCheck("test", "test")``` is t how you call Powershell functions - you want ```MatchCheck “test" "test"``` if you want to use positional parameters or ```MatchCheck -input_text_1 “test" -input_text_2 “test"``` to use named parameters. Either way, don’t use enclosing brackets. What you’re currently doing is passing an *array* with both strings in it as the *first* parameter and ```$null``` as the value for the second parameter. This is a multiple-duplicate question but I can’t get the links to others right now, if someone else wants to do the honours… – mclayton Jul 12 '23 at 16:25
  • 1
    Also, the brackets are unnecessary in your ```return($false)``` - ```return``` is a command rather than a function, and in any case you know now that you don’t use brackets for functions even if it were :-). – mclayton Jul 12 '23 at 16:29
  • 1
    In short: PowerShell functions, cmdlets, scripts, and external programs must be invoked _like shell commands_ - `foo arg1 arg2` - _not_ like C# methods - `foo('arg1', 'arg2')`. If you use `,` to separate arguments, you'll construct an _array_ that a command sees as a _single argument_. See the [linked duplicate](https://stackoverflow.com/q/4988226/45375) and [this answer](https://stackoverflow.com/a/65208621/45375) for more information. – mklement0 Jul 12 '23 at 16:35

0 Answers0