Windows Dev Center

Collapse the table of content
Expand the table of content
Expand Minimize
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

PowerShell Class

Provides methods that are used to create a pipeline of commands and invoke those commands either synchronously or asynchronously within a runspace. This class also provides access to the output streams that contain data that is generated when the commands are invoked. This class is primarily intended for host applications that programmatically use Windows PowerShell to perform tasks. This class is introduced in Windows PowerShell 2.0.

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

Dim instance As PowerShell

public sealed class PowerShell : IDisposable
public final class PowerShell implements IDisposable
public final class PowerShell implements IDisposable

The following example shows how to synchronously invoke a pipeline that contains a single command.

namespace HostSamples
  using System;
  using System.Management.Automation;  // Windows PowerShell namespace.

  /// <summary>
  /// This class defines the main entry point for a host application that 
  /// synchronously invokes the following pipeline: 
  /// [Get-Process]
  /// </summary>
  internal class HostPS1
    /// <summary>
    /// The PowerShell object is created and manipulated within the 
    /// Main method.
    /// </summary>
    /// <param name="args">This parameter is not used.</param>
    private static void Main(string[] args)
      // Call the PowerShell.Create() method to create an 
      // empty pipeline.
      PowerShell ps = PowerShell.Create();

      // Call the PowerShell.AddCommand(string) method to add 
      // the Get-Process cmdlet to the pipeline. Do 
      // not include spaces before or after the cmdlet name 
      // because that will cause the command to fail.

      Console.WriteLine("Process                 Id");

      // Call the PowerShell.Invoke() method to run the 
      // commands of the pipeline.
        foreach (PSObject result in ps.Invoke())
      } // End foreach.
    } // End Main.
  } // End HostPs1.

Host applications can use the PowerShell object to run any combination of cmdlets and scripts that can be invoked at the command line. By using this object, the host application can create a pipeline, add commands and scripts to the pipeline, specify the runspace where the commands are run, and then invoke the pipeline synchronously or asynchronously. For more information about host applications, see Writing a Windows PowerShell Host Application.

For more sample code, see Host Application Samples.


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