How to Wait for a Command to Finish in PowerShell?

As a PowerShell developer, you will like this tutorial. PowerShell is used to automate tasks and manage systems. However, sometimes, you need to ensure that one command completes before another starts. In this tutorial, I will explain how to wait for a command to finish in PowerShell with examples.

To make PowerShell wait for a command to finish, you can use the Start-Process cmdlet with the -Wait parameter. This method ensures that PowerShell waits for the specified process to complete before moving on to the next command. For example, Start-Process -FilePath “C:\Program Files\ExampleApp\ExampleApp.exe” -Wait will run ExampleApp.exe and pause execution until the application finishes running.

PowerShell Wait

PowerShell provides several ways to wait for a command or process to finish. These methods include

  • using Start-Process with the -Wait parameter
  • Wait-Process
  • Wait-Job
  • Start-Sleep.

Now, let me show you each method with examples.

Method 1: Using Start-Process with -Wait

The Start-Process cmdlet can start a process and wait for it to complete. This is particularly useful when you need to run an external program and wait for it to finish before proceeding.

Syntax:

Start-Process -FilePath "Path\To\Your\Program.exe" -Wait

Example:

Let me show you an example.

Start-Process -FilePath "C:\Program Files\ExampleApp\ExampleApp.exe" -Wait
Write-Output "ExampleApp has finished running."

In this example, PowerShell will wait for ExampleApp.exe to complete before moving on to the Write-Output command.

Check out Convert HTML to PDF in PowerShell

Method 2: Using Wait-Process

The Wait-Process cmdlet waits for one or more running processes to be stopped before it continues. This is useful when you need to wait for a specific process to finish.

Syntax:

Here is the syntax:

Wait-Process -Name "ProcessName"

Example:

Here is an example.

Start-Process -FilePath "notepad.exe"
Wait-Process -Name "notepad"
Write-Output "Notepad has closed."

Here, PowerShell will wait for Notepad to close before executing the Write-Output command.

Check out Add Credentials in PowerShell Script

How to Wait for a Command to Finish in PowerShell

Method 3: Using Wait-Job

When working with background jobs in PowerShell, you can use the Wait-Job cmdlet to wait for a job to complete.

Syntax:

Here is the syntax:

Wait-Job -Job $job

Example:

Let me show you an example.

$job = Start-Job -ScriptBlock {
    Start-Sleep -Seconds 10
    "Job Completed"
}

Wait-Job -Job $job
Receive-Job -Job $job
Write-Output "The job has finished."

In this example, PowerShell waits for the background job to complete before moving on to the Receive-Job and Write-Output commands.

Check out PowerShell Match Operator Examples

Method 4: Using Start-Sleep

Sometimes, you might want to wait for a specific period rather than waiting for a process or job. The Start-Sleep cmdlet in PowerShell suspends the activity in a script for the specified period.

Syntax:

Start-Sleep -Seconds <number>

Example:

Here is an example.

Write-Output "Waiting for 5 seconds..."
Start-Sleep -Seconds 5
Write-Output "Wait is over."

In this example, PowerShell waits for 5 seconds before proceeding to the next command.

Conclusion

In this tutorial, I explained how to make PowerShell wait for a command to finish using different methods, such as using Start-Process, Wait-Process, Wait-Job, or Start-Sleep, etc.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.