Information
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.

PSCommand Constructor ()

 

Updated: April 27, 2016

Applies To: Windows PowerShell

Initializes a new instance of the PSCommand class. This constructor is introduced by Windows PowerShell 2.0.

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

public PSCommand()

This object is used when you want to predefine the commands that will be invoked (synchronously or asynchronously by a PowerShell object. To invoke these commands, set the PowerShell.Commands property and call the Invoke or BeginInvoke methods.

The following example shows how to predefine a pipeline that contains the Get-Process cmdlet and invoke the pipeline synchronously.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;


namespace HostPS2
{
  class HostPS2
  {
    static void Main(string[] args)
    {
      // Using the PSCommand object, add the cmdlet 
      // to the command pipeline.
      PSCommand cmd = new PSCommand();
                cmd.AddCommand("Get-Process");

      // Using the PowerShell object, set the command
      // pipeline and invoke the pipeline synchronously.
      PowerShell ps = PowerShell.Create();
      ps.Commands = cmd;

      foreach (PSObject result in ps.Invoke())
      {
        Console.WriteLine("{0,-20}{1}",
                result.Members["ProcessName"].Value,
                result.Members["Id"].Value);
      } // End foreach.
    } // End Main.
  } // End HostPs2.
}

Return to top

Community Additions

Show: