RunspaceFactory.CreateRunspacePool Method ()

Creates a runspace pool that contains a single runspace. This runspace uses the default host and default runspace configuration. This method is introduced in Windows PowerShell 2.0.

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

Dim returnValue As RunspacePool

returnValue = RunspaceFactory.CreateRunspacePool

public static RunspacePool CreateRunspacePool ()
public static RunspacePool CreateRunspacePool ()
public static function CreateRunspacePool () : RunspacePool

Return Value

Returns a RunspacePool object that can be used to open and close a single runspace. When opened, the runspace uses the default host and runspace configuration provided by Windows PowerShell.

This example shows how to create a runspace pool object that has a single runspace, open the runspace, use a PowerShell object to run commands in the runspace, how to close the runspace, and then how to dispose the runspace pool object to free resources.

using (RunspacePool rsp = RunspaceFactory.CreateRunspacePool())
  // Create a PowerShell object to run the following command.
  //  get-process wmi*
  PowerShell gpc = PowerShell.Create();        
  // Specify the runspace to use and add commands.
  gpc.RunspacePool = rsp;

  // Invoke the command asynchronously. 
  IAsyncResult gpcAsyncResult = gpc.BeginInvoke();
  // Get the results of running the command. 
  PSDataCollection<PSObject> gpcOutput = gpc.EndInvoke(gpcAsyncResult);

  // Process the output.
  Console.WriteLine("The output from running the command: get-process wmi*");
  for (int i= 0; i < gpcOutput.Count; i++)
                     "Process Name: {0} Process Id: {1}",

A runspace pool is typically a set of runspaces that have the same features, such as the same host, initial session state, and connection information. A runspace from that pool is the operating environment used when invoking the commands of a pipeline. After the runspace pool is created, a runspace from the pool must be opened by using one of the following methods before it can be used:

  • To open a runspace from the pool synchronously, call the Open method.

  • To open a runspace from the pool asynchronously, call the BeginOpen method.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.


Target Platforms

© 2015 Microsoft