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)
'Usage
Dim instance As PowerShell

[SuppressMessageAttribute("Microsoft.Naming", "CA1724:TypeNamesShouldNotMatchNamespaces", Justification="PowerShell is a valid type in SMAR namespace.")] 
public sealed class PowerShell : IDisposable
/** @attribute SuppressMessageAttribute("Microsoft.Naming", "CA1724:TypeNamesShouldNotMatchNamespaces", Justification="PowerShell is a valid type in SMAR namespace.") */ 
public final class PowerShell implements IDisposable
SuppressMessageAttribute("Microsoft.Naming", "CA1724:TypeNamesShouldNotMatchNamespaces", Justification="PowerShell is a valid type in SMAR namespace.") 
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.
      ps.AddCommand("Get-Process");

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

      // Call the PowerShell.Invoke() method to run the 
      // commands of the pipeline.
        foreach (PSObject result in ps.Invoke())
      {
        Console.WriteLine(
                "{0,-24}{1}",
                result.Members["ProcessName"].Value,
                result.Members["Id"].Value);
      } // 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.


System.Object
  System.Management.Automation.PowerShell
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

Windows Developer Preview, Windows Server Developer Preview

Send comments about this topic to Microsoft.
Show:
© 2014 Microsoft. All rights reserved.