Search code examples
powershellpowershell-cmdlet

What's the difference between WriteObject(x, true) and multiple writeobjects when writing a PowerShell cmdlet?


I want to write a cmdlet that reads multiple records from a database and puts them onto the pipeline.

I think I can do either a single WriteObject(Enumerable<rec>, true) or I can loop myself and call WriteObject multiple times.

What's the difference between these two?


Solution

  • Here is the documentation: Cmdlet.WriteObject Method (Object, Boolean)

    And here is the example:

    # Writes objects one by one
    function Test1
    {
        [CmdletBinding()]param()
        $data | %{ $PSCmdlet.WriteObject($_) }
    }
    
    # Writes the collection and allows the core to enumerate it one level.
    # Numbers of written objects is the collection item count.
    function Test2
    {
        [CmdletBinding()]param()
        $PSCmdlet.WriteObject($data, $true)
    }
    
    # Writes the collection as a single object.
    # Numbers of written objects is 1.
    function Test3
    {
        [CmdletBinding()]param()
        $PSCmdlet.WriteObject($data, $false)
    }
    
    function Test
    {
        (Test1).GetType().Name
        (Test2).GetType().Name
        (Test3).GetType().Name
    }
    
    $data = New-Object System.Collections.ArrayList
    
    Write-Host "1 item"
    $null = $data.Add('hello')
    Test
    
    Write-Host "2+ items"
    $null = $data.Add('world')
    Test
    

    Output:

    1 item
    String
    String
    ArrayList
    2+ items
    Object[]
    Object[]
    ArrayList
    

    Thus, calling WriteObject(item) for each item in a collection is basically the same as WriteObject(items, true); in both cases the collection itself has gone.

    WriteObject(items, false) is different; it returns a reference to the collection and the caller can use that effectively depending on a scenario. For example, if a collection is a DataTable object (not unrolled set of DataRow items) then a caller can operate on DataTable members of the returned object.