Running PowerCLI Cmdlets Asynchronously

By default, PowerCLI cmdlets return an output only after completion of the requested tasks. If you want a cmdlet to return to the command line immediately, without waiting for the tasks to complete, you can use the RunAsync parameter.

When you use the RunAsync parameter, the cmdlet returns Task objects instead of its usual output. The Status property of a returned Task object contains a snapshot of the initial state of the task. This state is not updated automatically and has the values Error, Queued, Running, or Success. You can refresh a task state by retrieving the task object with the Get-Task cmdlet. If you want to observe the progress of a running task and wait for its completion before running other commands, use the Wait-Task cmdlet.

Note: In PowerCLI, the RunAsync parameter affects only the invocation of a cmdlet and does not control whether the initiated tasks run consecutively or in parallel. For example, the Remove-VM cmdlet might remove the selected virtual machines simultaneously or consecutively depending on the internal design of PowerCLI. To make sure that tasks initiated by a cmdlet run consecutively, run the cmdlet in a loop, each time applying it to a single object.

Running Remove-VM with and without the RunAsync parameter

Remove-VM $vmList

The command returns no output when all virtual machines stored in the $vmList variable are removed, irrespective of whether they are removed simultaneously.

Remove-VM $vmList -RunAsync

The command returns an output that consists of one or more Task objects immediately.