Export (0) Print
Expand All

Windows PowerShell Host Application Overview

For Windows PowerShell, a host application is any application that loads the Windows PowerShell engine into its process and uses it to perform operations. This could be a graphical user interface (GUI) that runs Windows PowerShell commands to perform an administrative task, or it could be a console application that provides an interactive session for the user.

Host applications provided by Windows PowerShell

Windows PowerShell provides two host applications: powershell.exe and Windows PowerShell Integrated Scripting Environment (ISE) (introduced by Windows PowerShell 2.0). Powershell.exe is the default host application. It is a console application that provides an interactive session. Windows PowerShell Integrated Scripting Environment (ISE) is a host application that provides an interactive scripting environment that includes interactive session and a GUI to support those sessions. For example, by using the GUI you can write, save, and debug scripts that can be run in the interactive session.

Communicating with Windows PowerShell

To communicate with Windows PowerShell, the host application uses its main function for primary access to the Windows PowerShell engine through a host interface. The host application can use the default interface that is provided by Windows PowerShell, or the application can create a custom interface. The host interface that is used by the application is also accessible to commands through the $Host variable, and is accessible to cmdlets that derive from the PSCmdlet base class through the Host property of the derived cmdlet classes.

Be aware that custom host application can run in a separate process or even on a separate computer from Windows PowerShell. However, the default host application (powershell.exe) always runs in the same process as the Windows PowerShell engine and on the same computer.

The Windows PowerShell API

Windows PowerShell 2.0 introduces the PowerShell class, which provides methods for defining a pipeline commands (cmdlets or scripts) and invoking that pipeline synchronously or asynchronously. In addition, properties of the PowerShell object can be used to specify the runspace or runspace pool that will be used when running the commands of the pipeline. Here is an example of a simple host application that uses this class to run the Get-Process cmdlet synchronously in the default runspace.

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 Application Terms

Here are some terms that are used in this document when discussing host applications.

Pipeline
The pipeline is the container for the cmdlets and scripts that are run programmatically by the host application. The pipeline can be constructed by using methods of the PowerShell class, or it can be predefined by using the methods of the PSCommand class.

Host
The host is the interface between the commands and the Windows PowerShell engine. Custom hosts can be specified when the application creates the runspace where the command pipeline is invoked. See Custom Host.

Host Application
A host application is any application that loads the Windows PowerShell engine into its process and uses it to perform operations.

Runspace
A runspace is the operating environment for the commands invoked by the host application. This environment includes the commands and data that are currently present, and any language restrictions that currently apply. Runspaces are created by using the System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace method. See Host Application Runspaces.

Remote Runspace
A runspace that uses WS-Management-based Windows PowerShell remoting to establish a remote connection. See Windows PowerShell Remoting.

Runspace Pool
A set of runspaces that share the same configuration. The host application can define a pool of runspaces once, and then a runspaces from the pool whenever a new runspace is needed. Runspaces are created by using the System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspacePool method. See Host Application Runspaces.

See Also



Community Additions

Show:
© 2014 Microsoft