powershellwait

Does the PowerShell call operator `&` wait for a command to finish?


We are using the call operator & to run an executable in an automated process:

& $pathToExecutable

Now it seems like PowerShell does not inherently wait for this executable to finish and continues script execution, leading to a failure of our automated process in some use cases. Does the call operator not wait for the started process to finish, or does it only wait under certain conditions or for a specific type of commands?

The documentation of the call operator does not mention if it waits or not, it simply says the command runs in a child scope. I feel like by default you would expect that it runs the command in a synchronous way and executes the next line once this command is finished.


Solution

  • EDIT(thanks @js2020 ):

    It depends on the executible.

    for example cmd.exe does wait while notepad.exe does not.

    write-host 1
    & cmd.exe /c timeout 5
    write-host 2
    
    write-host 3
    & notepad.exe $PROFILE
    write-host 4
    

    ORIGINAL:

    It waits.

    try

    write-host 1
    & cmd.exe /c timeout 5
    write-host 2