Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell functions return behavior

Tags:

powershell

I am seeing some rather weird behavior with PowerShell, it looks like custom functions might need a "parenthesis wrapper" to evaluate as you might expect them. Given a simple PowerShell function:

function Return-True { return $true }

and then some sample code to invoke it:

PS C:\> Return-True
True
PS C:\> Return-True -eq $false
True
PS C:\> (Return-True) -eq $false
False

Ideas? Comments?

like image 299
Goyuix Avatar asked Sep 29 '08 15:09

Goyuix


People also ask

How does a function return a value in PowerShell?

The return keyword exits a function, script, or script block. It can be used to exit a scope at a specific point, to return a value, or to indicate that the end of the scope has been reached.

Can we return multiple values from a function in PowerShell?

Since PowerShell does not apply any restriction on data type returned, it created a lot of possibilities on what can be returned as an output of the function. So if one needs to return multiple values or objects, it is generally suggested to create an array of the objects and then return the array.

How do you press a return in PowerShell?

The tilde (~) represents the ENTER keystroke. You can also use {ENTER} , though they're not identical - that's the keypad's ENTER key.

What is $? PowerShell?

$? Contains the execution status of the last command. It contains True if the last command succeeded and False if it failed. For cmdlets and advanced functions that are run at multiple stages in a pipeline, for example in both process and end blocks, calling this.


2 Answers

When PowerShell sees the token Return-True it identifies it as a command and until evaluation or end of the statement, everything else is an argument which is passed to the function Return-True.

You can see this in action if you do:

PS > function Return-True { "The arguments are: $args"; return $true }
PS > Return-True -eq $false
The arguments are: -eq False
True

That's why all of the following return 'True', because all you are seeing is the result of calling Return-True with various arguments:

PS > Return-True -eq $false
True
PS > Return-True -ne $false
True
PS > Return-True -eq $true
True
PS > Return-True -ne $true
True

Using (Return-True) forces PowerShell to evaluate the function (with no arguments).

like image 168
Grant Wagner Avatar answered Sep 29 '22 05:09

Grant Wagner


The second line is not doing a boolean evaluation. Look at what happens if you do the same thing with strings.

PS C:\> function Return-True { return "True string" }
PS C:\> Return-True
True string
PS C:\> Return-True -eq "False string"
True string
PS C:\> (Return-True) -eq "False string"
False

The second line is simply returning the value of the function, and not doing a comparison. I'm not sure exactly why this behavior is happening, but it makes the behavior easier to see than when using boolean values that are being converted to the strings "True" and "False".

like image 42
hurcane Avatar answered Sep 29 '22 06:09

hurcane