PowerShell functions that return true/false

95,608

Solution 1

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"
}

Solution 2

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.

Solution 3

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) { ... }
 }

Solution 4

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)
Share:
95,608
scapegoat17
Author by

scapegoat17

Updated on March 02, 2021

Comments

  • scapegoat17
    scapegoat17 about 3 years

    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...
     }
    
  • scapegoat17
    scapegoat17 almost 11 years
    Looks good to me! I cant try it out just yet, but it seems like pretty solid logic. Thanks!
  • Kappacake
    Kappacake almost 6 years
    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"
  • kkuilla
    kkuilla over 4 years
    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 .
  • Mike Q
    Mike Q over 3 years
    This is the best answer
  • lolsky
    lolsky about 3 years
    It looks like the link for Boolean Values and Operators is broken. Has anyone found an updated link? Edit: This might be the link.
  • antonyoni
    antonyoni about 3 years
    @lolsky yes, that's the one. I've updated it. Thank you.