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
Conclusion: Success if the file exists, and Fail if it does not.
One warning: PowerShell functions return everything that has not been written. For example, if I change the Do-Something code to:
Function Do-Something { "Hello" $return = Test-Path c:\dev\test.txt return $return }
Then the return will always be successful, because even when the file does not exist, the Do-Something function returns an array of objects ("Hello", False). Take a look at booleans and operators for more information on booleans in PowerShell.
source share