- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PowerShell: Wait for the First command to finish
We know that PowerShell executes commands sequentially until we specify some parallel Jobs but sometimes the next command executes before the First command because the first command might be taking a long time to retrieve the data. In that case, if you want the previous command to finish first and then the next command to get executed, you can use PowerShell Job functionality.
For example, we need to write a script to ask for the user input to terminate process ID but the program should retrieve the process IDs first.
Example
$job = Start-Job {Get-Process} Wait-Job $job | Out-Null Receive-Job $job $id = Read-Host "Enter the Process ID to terminate the Process " Stop-Process -Id $id -PassThru -Verbose
In the above example, we are starting a PowerShell process job inside the Start-Job command and waiting for it to proceed further until it gets executed using the Wait-Jobcommand. Once Get-Process
command execution completes, the output will be displayed using the Receive-Job command and at the next, it asks for the process ID to get it terminate the specific process.
- Related Articles
- How to wait for a goroutine to finish in Golang?
- Bash wait Command with Examples
- How to use Wait-Process in Powershell?
- How to use the Timeout command in PowerShell?
- How to use the tree command in PowerShell?
- How to use the Set-Location command in PowerShell?
- How to run PowerShell commands from the command prompt?
- How to use –Wait parameter in Restart-Computer cmdlet in PowerShell?
- How to use PowerShell Break statement with the Switch command?
- How to use the ConvertFrom-StringData command in PowerShell?\n
- How to run a PowerShell script from the command prompt?
- How to convert command output to the Hashtable format in PowerShell?
- How to run Invoke-Command in PowerShell Workflow?
- How to use Split-Path command in PowerShell?
- How to change the Title of the console using PowerShell command?
