I’ve previously posted on how to call use a PowerShell script to call another PowerShell script, even with a variable passed! What about returning a value to the calling PowerShell script? This is actually pretty easy and intuitive for a single variable. In my case, I want to know if the called script failed or not.
This first script simply calls the second script, test2.ps1 and sets its result to $return. Then I echo back the $return value to make sure it stuck.
$return = & “c:\script2.ps1”
This second script simply prints text to prove it was called, then returns back $true.
Write-Host “Hello World!” -back yellow -fore green
And this is the result:
PS > ./test1.ps1
There are no doubt more sophisticated means to return multiple values and even objects back, which may or may not be the same thing as I’ve given above, but this sufficed to meet a need I had to just pass back a complete/fail variable.