PowerShell.BeginInvoke<TInput, TOutput> Method (PSDataCollection<TInput>, PSDataCollection<TOutput>)


Updated: April 27, 2016

Applies To: Windows PowerShell

Asynchronously runs the commands of the PowerShell object pipeline by using a specified input buffer for the first command of the pipeline and a specified output buffer for the results of the invocation. This method is introduced in Windows PowerShell 2.0.

Namespace:   System.Management.Automation
Assembly:  System.Management.Automation (in System.Management.Automation.dll)

public IAsyncResult BeginInvoke<TInput, TOutput>(
	PSDataCollection<TInput> input,
	PSDataCollection<TOutput> output


Type: System.Management.Automation.PSDataCollection<TInput>

A PSDataCollection<T> of type parameter TInput that contains the input data for the first command of the pipeline.

Type: System.Management.Automation.PSDataCollection<TOutput>

A PSDataCollection<T> of type parameter TOutput that contains the output of the pipeline.

Return Value

Type: System.IAsyncResult

An IAsyncResult interface that represents the status of the asynchronous operation. The EndInvoke method uses this interface to determine when to return the results of the operation.

Type Parameters


The type of the objects used as input to the first command of the pipeline.


The type of the output objects expected from invoking the commands of the pipeline.

Exception Condition

Cannot perform the operation because the operation is already started, or there are no commands in the pipeline.


The PowerShell object is disposed.

When providing input data, the input buffer must be closed before the asynchronous invocation of the pipeline can be completed. To close the input buffer, the caller of the BeginInvoke<TInput, TOutput> method must call the Complete method after all input is written to the buffer. If you do not call the Complete method, Windows PowerShell will continue to wait for input data and not invoke the pipeline. If you observe that the invocation is not doing anything, this might be the reason.

The following example asynchronously runs the Sort-Object cmdlet based on input from a supplied input buffer, returning the results to the specified output buffer.

// Use the PowerShell.Create and Powers.AddCommand 
// methods to create a command pipeline that includes the  
// Sort-Object cmdlet. Do not include spaces immediatly 
// before or after the cmdlet name as that will cause 
// the command to fail.
PowerShell ps = PowerShell.Create().AddCommand("Sort-Object");

// Create the data to be sorted by the Sort-Object cmdlet.
PSDataCollection<int> input =
      new PSDataCollection<int>();

// Create the output buffer for the results.
PSDataCollection<PSObject> output =
      new PSDataCollection<PSObject>();

// Create an IAsyncResult object and call the
// BeginInvoke method to start running the 
// command pipeline asynchronously.
IAsyncResult async = 

// Using the PowerShell.EndInvoke method, run the command
// pipeline using the default runspace.
foreach (PSObject result in output)
} // End foreach.

Return to top

Community Additions