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.

PowerShell.Invoke Method ()

 

Updated: April 27, 2016

Applies To: Windows PowerShell

Synchronously runs the commands of the PowerShell object pipeline. This method is introduced in Windows PowerShell 2.0.

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

public Collection<PSObject> Invoke()

Return Value

Type: System.Collections.ObjectModel.Collection<PSObject>

A Collection<T> collection of PSObject objects that contain the output of the pipeline.

Exception Condition
InvalidOperationException

Cannot perform the operation because the command is already started. Stop the command and try the operation again.

-or-

The pipeline is empty.

ScriptCallDepthException

The script recursed too deeply into script functions. There is a fixed limit on the depth of recursion.

SecurityException

A common language runtime (CLR) security violation occurred. Typically, this happens because the current CLR permissions do not allow adequate access for reflecting on a cmdlet assembly.

ThreadAbortException

The thread in which the call was executing was aborted.

RuntimeException

The call threw a run-time exception.

ParameterBindingException

One of more parameters or parameter values specified for a cmdlet are not valid, or mandatory parameters for a cmdlet were not specified.

CmdletInvocationException

A cmdlet generated a terminating error.

CmdletProviderInvocationException

A provider cmdlet generated a terminating error.

ActionPreferenceStopException

The ActionPreference.Stop or ActionPreference.Inquire policy triggered a terminating error.

PipelineStoppedException

The pipeline was terminated asynchronously.

MetadataException

A terminating error occurred while the metadata for dynamic parameters was being generated.

ObjectDisposedException

The PowerShell object is disposed.

The commands of the pipeline can be accessed by using the Commands property.

The following example defines a pipeline that contains the Get-Process cmdlet, and invokes the pipeline synchronously.

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


namespace HostPS1
{
  class HostPS1
  {
    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, 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 in the default runspace.
        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.
}

The following example defines a pipeline that contains two cmdlets and invokes the pipeline synchronously.

using System;
using System.Management.Automation;

namespace HostPS1c
{
  class HostPS1c
  {
    static void Main(string[] args)
    {
      // Using the PowerShell object, create the command 
      // pipeline and add the cmdlet and the argument for 
      // the positional parameter.
      PowerShell ps = PowerShell.Create();
      ps.AddCommand("Get-Process");
      ps.AddArgument("wmi*");
      ps.AddCommand("Sort-Object");
      ps.AddParameter("descending");
      ps.AddArgument("id");

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

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

Return to top

Community Additions

Show: