WriteObject Method (Object, Boolean)
Collapse the table of content
Expand the table of content

Cmdlet.WriteObject Method (Object, Boolean)

 

Updated: April 27, 2016

Applies To: Windows PowerShell

Writes an object to the output pipeline that can be enumerated by Windows PowerShell.

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

public void WriteObject(
	object sendToPipeline,
	bool enumerateCollection
)

Parameters

sendToPipeline
Type: System.Object

The object to be sent to the pipeline.

enumerateCollection
Type: System.Boolean

True indicates that Windows PowerShell runtime will enumerate the object one level. The default is False.

Exception Condition
PipelineStoppedException

The pipeline is stopped. The pipeline was terminated either before the call was made or while the call was in progress. By default, the cmdlet should allow the caller of the processing record method to catch this exception.

InvalidOperationException

The call cannot be completed at this time or cannot be completed from this thread. For more information, see the following Remarks section.

This method can be called only from within the cmdlet implementation of the BeginProcessing, ProcessRecord, and EndProcessing methods and only from that thread. If this call is made from outside these implementations or from another thread, an InvalidOperationException exception is thrown.

For more information about how to send an object that is not a collection, see the Cmdlet.WriteObject method.

Cmdlets can also write to the debug, error, progress, warning, and verbose data streams by using the following methods:

For more information about cmdlets, see Windows PowerShell Cmdlets.

WriteObject is used in a number of locations throughout cmdlet code samples. The following, taken from the Runspace10 sample in the PowerShell SDK, passes the process object on to the pipeline.

protected override void ProcessRecord()
{
  // Get the current processes.
  Process[] processes = Process.GetProcesses();

  // Write the processes to the pipeline making them available
  // to the next cmdlet. The second argument (true) tells the 
  // system to enumerate the array, and send one process object 
  // at a time to the pipeline.
  WriteObject(processes, true);
}
Return to top

Community Additions

ADD
Show:
© 2016 Microsoft