How to Create a Runspace

This example shows how to create an individual runspace that can be used to run the commands of a pipeline. In this example, the host application creates a runspace but does not add any configuration information, such as specifying a custom host or an initial session state. When the runspace is opened, it uses only the default information provided by Windows PowerShell.

The host application described in the following procedure is implemented as a console application by using Microsoft Visual Studio.

To create a runspace

  1. With the console application project open, add a reference to the System.Management.Automation.dll assembly.

    noteNote:
    • For Windows XP and Windows Vista operating systems, the assembly is located in the Windows PowerShell installation directory ($PSHome).

    • For Windows 7 operating systems, the assembly is located in the following folder: Windows\assembly\GAC_MSIL\System.Management.Automation.

  2. Add a using statement that references the System.Management.Automation namespace, and remove the default using statements that are not needed. The following statements are the only using statements that are needed for this host application.

  3. Within the Main method of the host application class, call the CreateRunspace method to create the runspace and the Open method to open the runspace. The runspace must be opened before it can be used by the host application.

    Runspace rs = RunspaceFactory.CreateRunspace();
    rs.Open();
    
    
  4. Create a PowerShell object and define the command to run. Call the Create method to create the object. Next, set the Runspace property to specify the runspace. Then, call the appropriate methods to create a command. This example calls the AddCommand and AddArgument methods to create the following command: Get-Process wmi*.

    PowerShell ps = PowerShell.Create();
    ps.Runspace = rs;
    ps.AddCommand("Get-Process").AddArgument("wmi*");
    
    
  5. Call the Invoke method to run the pipeline synchronously. The commands of the pipeline will be run in the specified runspace.

    foreach (PSObject result in ps.Invoke())
    {
      Console.WriteLine("{0,-20}{1}",
              result.Members["ProcessName"].Value,
              result.Members["Id"].Value);
    } // End foreach.
    
    
  6. Call the Close method to free resources that are used to create the runspace.

    rs.Close();
    
    

Example

This example shows how to create a runspace, open the runspace, and then synchronously invoke the Get-Process wmi* command within the opened runspace.

namespace HostPS4
{
    using System;
    using System.Management.Automation;           // Windows PowerShell namespace.
    using System.Management.Automation.Runspaces; // Windows PowerShell namespace.

  class HostPS4
  {
    static void Main(string[] args)
    {

    // Call the CreateRunspace() method to create the 
    // runspace. This call does not specify any 
    // configuration information such as a custom host 
    // or initial session state information.
    Runspace rs = RunspaceFactory.CreateRunspace();
    rs.Open();


      // Call the Create() method to create the PowerShell 
      // object, and then specify the runspace and 
      // create the pipeline.
      PowerShell ps = PowerShell.Create();
      ps.Runspace = rs;
      ps.AddCommand("Get-Process").AddArgument("wmi*");

      Console.WriteLine("Process               Id");
      Console.WriteLine("------------------------");


      // Using the PowerShell object, invoke the pipeline 
      // synchronously.
      foreach (PSObject result in ps.Invoke())
      {
        Console.WriteLine("{0,-20}{1}",
                result.Members["ProcessName"].Value,
                result.Members["Id"].Value);
      } // End foreach.

      // Close the runspace and release any resources.
      rs.Close();
    } // End Main.
  } // End HostPS4.
}

For complete samples of other ways to define runspaces, see Runspace Samples.

See Also



Show:
© 2014 Microsoft