6

How does the behaviour differ between & powershell .\other.ps1 and & .\other.ps1?

Edit: In particular, how do they differ if there's an error in other.ps1?

1 Answer 1

12

You get another PowerShell process in the former case and the script cannot read variables defined in your current session:

PS> $foo = 'bar'
PS> 'Write-Host $foo'|Set-Content x.ps1
PS> & powershell .\x.ps1

PS> & .\x.ps1
bar
Sign up to request clarification or add additional context in comments.

1 Comment

@ColonelPanic: Just like variables, exceptions will not cross process boundaries. Change the contents of x.ps1 to throw error, and compare the output of try { powershell.exe ./x.ps1; "success? $?; $LastExitCode" } catch { "caught $_; success? $?; $LastExitCode" } and try { ./x.ps1; "success? $?; $LastExitCode" } catch { "caught $_; success? $?; $LastExitCode" }.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.