21

I am pretty new with using PowerShell and was wondering if anyone would have any input on trying to get PowerShell functions to return values.

I want to create some function that will return a value:

 Function Something
 {
     # Do a PowerShell cmd here: if the command succeeded, return true
     # If not, then return false
 }

Then have a second function that will only run if the above function is true:

 Function OnlyTrue
 {
     # Do a PowerShell cmd here...
 }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
scapegoat17
  • 5,509
  • 14
  • 55
  • 90
  • For handling the "not"/false operator, see: http://stackoverflow.com/questions/8095638/how-do-i-negate-a-condition-in-powershell – storm_m2138 Mar 13 '14 at 17:20

4 Answers4

26

Don't use True or False, instead use $true or $false

function SuccessConnectToDB {
 param([string]$constr)
 $successConnect = .\psql -c 'Select version();' $constr
    if ($successConnect) {
        return $true;
    }
    return $false;
}

Then call it in a nice clean way:

if (!(SuccessConnectToDB($connstr))) {
    exit  # "Failure Connecting"
}
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
15

You can use return statements in PowerShell:

Function Do-Something {
    $return = Test-Path c:\dev\test.txt
    return $return
}

Function OnlyTrue {
    if (Do-Something) {
        "Success"
    } else {
        "Fail"
    }
}

OnlyTrue

The output is Success if the file exists and Fail if it doesn't.

One caveat is that PowerShell functions return everything that's not captured. For instance, if I change the code of Do-Something to:

Function Do-Something {
    "Hello"
    $return = Test-Path c:\dev\test.txt
    return $return
}

Then the return will always be Success, because even when the file does not exist, the Do-Something function returns an object array of ("Hello", False). Have a look in Boolean Values and Operators for more information on booleans in PowerShell.

antonyoni
  • 849
  • 5
  • 11
  • 3
    For future readers, I got caught on the caveat. I was expecting my function to return `$false` but I always received `@($false,$false)`. This was because I did not capture the output from one function call into a variable . – kkuilla Oct 25 '19 at 13:35
  • It looks like the link for [Boolean Values and Operators](https://stackoverflow.com/questions/18148560/powershell-functions-that-return-true-false) is broken. Has anyone found an updated link? Edit: [This](https://devblogs.microsoft.com/powershell/boolean-values-and-operators/) might be the link. – lolsky Mar 01 '21 at 02:18
  • @lolsky yes, that's the one. I've updated it. Thank you. – antonyoni Mar 01 '21 at 17:37
6

You'd do something like this. The Test command uses the automatic variable '$?'. It returns true/false if the last command completed successfully (see the about_Automatic_Variables topic for more information):

Function Test-Something
 {
     Do-Something
     $?
 }

 Function OnlyTrue
 {
     if(Test-Something) { ... }
 }
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • Looks good to me! I cant try it out just yet, but it seems like pretty solid logic. Thanks! – scapegoat17 Aug 09 '13 at 14:28
  • 1
    I tried your code, but it returns "False \n\r True". I changed "Do-Something" with "Get-Item dhjsiadosajdiosa", which is an example of a command that fails. I would expect the script to return "False", but it returns "False \n\r True" – Kappacake Aug 16 '18 at 14:25
2

Very delayed answer but just had the same problem in powershell 5. You can use 1 and 0 as return values. then you can convert it to boolean or just use "-eq 1" or 0

Function Test
{
   if (Test-Path c:\test.txt){
      return 0
   }else{
      return 1
   }
}

[bool](Test)