Passing DataTables Through the PowerShell Pipeline

Here’s a function that builds a small DataTable and ends with $table. The caller gets something different depending on how many rows the table holds:

A woman DBA works a lever where a pipe splits, sending gold data tiles one at a time along one branch and a whole tray of tiles inside a glass case along the other.

None of those is a DataTable. Code that expects one has to cope with three different shapes, and which shape it gets depends on the data.

In Turning a Folder of DBA Scripts into a PowerShell Module, my Invoke-SatQuery function ended with $table.Rows, which behaves the same way. After that post went up, James Brundage (@mrpowershell.com on Bluesky) replied that a leading comma before the returned value keeps the DataTable, because PowerShell enumerates the rows by default. He added that keeping the table lets you discover the output’s columns and schema.[1] In a second reply, he said the table’s Compute and Select methods are faster than Measure-Object and Select-Object, that Write-Output -NoEnumerate is a less arcane way to skip the enumeration, and that $PSCmdlet.WriteObject($table, $false) is faster.[1] I replied that I’d sent rows on purpose, because every caller in that post pipes into Select-Object.

That post now has a short section on the choice. Here I look at why PowerShell unrolls the table, which ways of returning it work, what the next command in the pipeline receives, and what you can do with the table once you have it. I ran every test against DataTables built in memory, in Windows PowerShell 5.1.26100.9711 and PowerShell 7.6.9. Apart from the timings, both editions gave the same results.

Why the Table Turns into Rows

Everything a function outputs goes down the pipeline, with or without return. When that output is a collection, PowerShell unrolls it and passes the members along one at a time.[2] A DataTable gets the same treatment, because the pipeline enumerates a DataTable through its Rows property.[3]

The caller then collects whatever came out. In my tests, no rows gave $null, one row gave that DataRow on its own, and three rows gave an Object[] array of DataRows.

Three Ways to Keep the Table

To send the table as one object, stop the enumeration where the function writes it. Write-Output -NoEnumerate passes a collection down the pipeline as a single object.[4] This version uses it on the last line. I also made it an advanced function, so that $PSCmdlet, which comes up below, works in it too:

Two other last lines give the same result. , $table wraps the table in a one-element array. PowerShell unrolls the outer array, and the table comes out intact. The arrays deep dive on Microsoft Learn, originally written by Kevin Marquette, calls the comma “more of a hack” and offers Write-Output -NoEnumerate as the alternative.[5]

$PSCmdlet.WriteObject($table, $false) calls the method that compiled cmdlets use. When the second argument is $true, the method writes each member of a collection on its own, and $false turns that off.[6] $PSCmdlet represents the cmdlet or advanced function that’s running,[7] so it only works in an advanced function, such as one with [CmdletBinding()]. When I left that attribute off, in a script that wasn’t advanced either, both editions stopped with “You cannot call a method on a null-valued expression.”

I prefer Write-Output -NoEnumerate, because the parameter name tells the next reader what the line is for.

I also tried these last lines. Only the last row of the table kept the DataTable:

Last line of the function Rows: 0 Rows: 1 Rows: 3
$table or $table.Rows $null DataRow Object[] of DataRows
return ( $table ) $null DataRow Object[] of DataRows
return @( $table ) $null DataRow Object[] of DataRows
return [System.Data.DataTable]$table $null DataRow Object[] of DataRows
, $table.Rows DataRowCollection DataRowCollection DataRowCollection
, $table, Write-Output -NoEnumerate $table or $PSCmdlet.WriteObject($table, $false) DataTable DataTable DataTable

return $table does the same as $table followed by return: it writes the value and then exits.[2] Parentheses and the cast don’t change the value, so PowerShell unrolls it as usual. @( ) enumerates the rows into a new array, and PowerShell then unrolls that array. , $table.Rows keeps the collection, but you get a DataRowCollection rather than the DataTable.

Protect Every Function Boundary

The unrolling happens each time a function writes output. If a helper builds the table and returns it unprotected, the outer function already has rows by the time it could protect them. Get-Result protects its own output here, and New-ResultTable doesn’t:

That printed Object[] in both editions. Inside Get-Result, $table already holds an array of two DataRows, and Write-Output -NoEnumerate passes that array through as it is. After I changed the last line of New-ResultTable to Write-Output -NoEnumerate $table, Get-Result returned a DataTable.

What the Next Command Receives

Keeping the table at the function boundary also changes what the next command in a pipeline gets. Using the Get-SampleTable that ends with Write-Output -NoEnumerate:

When you pipe the function straight into another command, that command receives one object, the DataTable. A variable holds the DataTable, but piping the variable unrolls it into rows again. Wrapping the call in parentheses has the same effect.

Get-SampleTable -RowCount 3 | Select-Object db, hours returned one object with empty db and hours values. Select-Object looked for those properties on the DataTable, which doesn’t have them. That’s why the functions in the module post send rows: their callers pipe straight into Select-Object. If you return the table, the caller can pipe $result.Rows instead.

Offering Both with -AsDataTable

A function can stream rows by default and return the table when the caller asks for it:

Without the switch, I got $null, one DataRow and an Object[] of DataRows for 0, 1 and 3 rows, and Select-Object worked as before. With it, I got a DataTable every time, including the empty one.

What You Can Do with the Table

Once the caller has the table, Columns lists each column’s name and .NET type, as in the output above. Rows.Count returns 0 for an empty result, where the rows form gives $null. The table also has two methods that do work you’d otherwise hand to pipeline cmdlets.

Compute takes an expression and a filter, and it computes the expression on the rows that pass the filter. The expression must use an aggregate function such as SUM or MAX.[8]

Select takes a filter, plus an optional sort order, and returns an array of the DataRow objects that match.[9] Despite the name, it filters rows like Where-Object. It doesn’t pick columns the way Select-Object does.

I timed each method against the cmdlet that does the same job on the same 100,000-row table, as the mean of 20 calls. These numbers come from one run on my workstation:

Operation 5.1 (ms) 7.6 (ms)
$big.Compute('MAX(hours)', '') 4.8 4.2
Measure-Object hours -Maximum on $big.Rows 440 270
$big.Select('hours > 99000') 49 44
Where-Object hours -gt 99000 on $big.Rows 463 282

Compute ran about 90 times faster than Measure-Object in 5.1, and about 64 times faster in 7.6. Select ran about 9 times faster than Where-Object in 5.1, and about 6.5 times faster in 7.6. Both pairs returned the same answers: a maximum of 100,000, and 1,000 matching rows.

Speed of the Output Methods

To check the claim about WriteObject, I timed four small functions, 20,000 calls each. Every function builds a 3-row table and then writes it with a different last line, so each figure includes building the table:

Last line 5.1 (microseconds per call) 7.6 (microseconds per call)
$table (rows, for comparison) 221 68
Write-Output -NoEnumerate $table 264 65
$PSCmdlet.WriteObject($table, $false) 206 57
, $table 212 51

In this run, WriteObject beat Write-Output -NoEnumerate in both editions, by about 58 microseconds per call in 5.1 and 8 in 7.6. The order of all four wasn’t the same in both editions, and in 7.6 they came within 17 microseconds of each other. At 58 microseconds a call, the gap adds up to about 1.2 seconds over 20,000 calls. For a function that waits even 2 milliseconds for SQL Server to answer a query, 58 microseconds is under 3% of the call.

What I Tested and What I Didn’t

  • One workstation, Windows PowerShell 5.1.26100.9711 and PowerShell 7.6.9.
  • DataTables built in memory with two columns. None of them came from a query.
  • The timings come from one run. Expect different numbers on your machine. The Compute and Select ratios held in both editions, but the order of the four output forms didn’t, so don’t read much into gaps of a few microseconds.
  • I timed Compute and Select only on the 100,000-row table, not on small ones.
  • I didn’t test DataSets, DataViews or memory use.

Try It Yourself

Take a function that returns query results, and call it with a query that returns no rows, then one, then several. Check $result.GetType().Name each time. If the caller needs the table, change the last line to Write-Output -NoEnumerate $table, and then check every function between the query and the caller.

Thanks to James for the replies that led to this post. If you return DataTables from your own functions, I’d like to hear which form you use and why. Find me on Bluesky or LinkedIn.

References

  1. James Brundage (@mrpowershell.com) - Bluesky. In two replies to the module post on 2026-11-04, suggested a leading comma to keep the DataTable and discover its columns and schema, and added that the table’s Compute and Select methods are faster than Measure-Object and Select-Object, that Write-Output -NoEnumerate is a less arcane way to skip enumeration, and that $PSCmdlet.WriteObject($table, $false) is faster. ↩
  2. about_Return - Microsoft Learn. States that the results of each statement are returned as output even without the return keyword, that return $a returns the same value as $a followed by return, and that PowerShell unrolls a collection returned from a function and passes the members through the pipeline one at a time. ↩
  3. about_Pipelines - Microsoft Learn. States that PowerShell automatically enumerates types that implement IEnumerable, and that it also enumerates System.Data.DataTable types through the Rows property. ↩
  4. Write-Output - Microsoft Learn. Write-Output enumerates objects in a collection by default, and the NoEnumerate parameter passes a collection down the pipeline as a single object. ↩
  5. Everything you wanted to know about arrays - Microsoft Learn. Originally written by Kevin Marquette. Describes the leading comma as “more of a hack”, and recommends Write-Output -NoEnumerate $array or return ,$array when a function’s array output must not be unwrapped. ↩
  6. Cmdlet.WriteObject Method - Microsoft Learn. For WriteObject(Object, Boolean): “If the object is a collection and the enumerateCollection flag is true, the objects in the collection will be written individually.” ↩
  7. about_Automatic_Variables - Microsoft Learn. States that $PSCmdlet contains an object that represents the cmdlet or advanced function that’s being run. ↩
  8. DataTable.Compute(String, String) Method - Microsoft Learn. “Computes the given expression on the current rows that pass the filter criteria.” The remarks state that the expression parameter requires an aggregate function. ↩
  9. DataTable.Select Method - Microsoft Learn. Select(String) “Gets an array of all DataRow objects that match the filter criteria”, and Select(String, String) returns them in the specified sort order. ↩