A function returns everything that is not captured by something else.
If u use the return keyword, every statement after the return line will not be executed!
Like this:
Function Test-Function
{
Param
(
[switch]$ExceptionalReturn
)
"Start"
if($ExceptionalReturn){Return "Damn, it didn't work!"}
New-ItemProperty -Path "HKCU:\\" -Name "test" -Value "TestValue" -Type "String"
Return "Yes, it worked!"
}
Test-Function
Will return:
Test-Function -ExceptionalReturn Will return:
If you do it like this:
Function Test-Function
{
Param
(
[switch]$ExceptionalReturn
)
. {
"Start"
if($ExceptionalReturn)
{
$Return = "Damn, it didn't work!"
Return
}
New-ItemProperty -Path "HKCU:\\" -Name "test" -Value "TestValue" -Type "String"
$Return = "Yes, it worked!"
Return
} | Out-Null
Return $Return
}
Test-Function
Will return:
Test-Function -ExceptionalReturn Will return:
With this trick you can control the returned output even if you are not sure what will each statement will spit out.