How to Wait for a Process to Finish in a PowerShell Script
Waiting for a process to finish before continuing is important in scripts that must not proceed until a task completes, such as waiting for an installer or a long operation. PowerShell provides a clean way to pause a script until a process ends.
The Command
Wait-Process -Name "setup"
What It Does
`Wait-Process` pauses the script until the named process, here “setup”, has finished running. This is useful when a script launches something and must wait for it to complete before moving on. The script TANGKAS39 simply halts at this line until the process ends, then continues, ensuring steps run in the correct order.
When You’d Use This
This is essential in scripts that must not continue until a launched process finishes, such as waiting for an installer to complete before configuring the installed program, or letting a long operation finish before the next step. It keeps script steps in the correct order when one depends on the previous one having fully completed.
Useful Variations
To wait on a process you just started, capture it when launching: `$p = Start-Process setup -PassThru` then `Wait-Process -Id $p.Id`, which waits for that exact instance. To add a time limit, use `-Timeout` followed by seconds, so the wait gives up after a set period rather than waiting indefinitely.
If It Doesn’t Work
If it errors that the process is not running, the process may have already finished or never started, so ensure it is launched first. Waiting by name is unreliable when several processes share a name, so capture the specific instance with `Start-Process -PassThru` and wait on its ID instead. Add `-Timeout` with seconds to avoid waiting forever if the process never ends.
Good to Know
If the named process is not running when `Wait-Process` runs, it reports an error rather than waiting, so ensure the process has started first. Using `-PassThru` with `Start-Process` to capture the specific instance is more reliable than waiting by name when multiple processes could share the name.
Putting It Together
Once you have run it once or twice, this becomes second nature. As part of understanding and controlling what runs on your PC, this command is one you will return to whenever the system feels slow or a program misbehaves. Paired with the related process commands, it gives you a full command-line alternative to Task Manager for diagnosing and managing what is running. Like anything in the terminal, the real value comes from trying it on your own system and adapting the variations above to what you actually need, so it is worth experimenting with in a safe, low-stakes situation before relying on it in a script or during troubleshooting. Keeping a note of the commands you find most useful, along with the variations that fit your workflow, turns scattered one-off tricks into a personal reference you can draw on whenever a similar task comes up again.