PowerShell function will not return DataTable
<p>I've got a PowerShell script on PowerShell v4.0 (Windows 7 x64 SP1) that creates a pretty complex DataTable.  I wanted to be able to place that DataTable code anywhere pretty easily, so I decided to wrap it in a simple function, like so:</p>

<pre><code>function Get-UserDataTable
{
    $DataTable = New-Object -TypeName System.Data.DataTable -ArgumentList 'User';

    $NewColumn = $DataTable.Columns.Add('Id',[System.Int32]);
    $NewColumn.AllowDBNull = $false;
    $NewColumn.Unique = $true;

    $NewColumn = $DataTable.Columns.Add('FirstName',[System.String]);
    $NewColumn.MaxLength = 64;

    $NewColumn = $DataTable.Columns.Add('MiddleName',[System.String]);
    $NewColumn.MaxLength = 64;

    $NewColumn = $DataTable.Columns.Add('LastName',[System.String]);
    $NewColumn.MaxLength = 64;

    return $DataTable;
}
</code></pre>

<p>However, that code always returns a null object.  I tried <code>Write-Output $DataTable</code>, <code>return $DataTable.Copy()</code>, and <code>$DataTable</code>, but the function is still always null.</p>

<p>So, I thought I might try adding some rows.  I can always clear the DataTable, and it'd still be less coding this way:</p>

<pre><code>function Get-UserDataTable2
{
    $DataTable = New-Object -TypeName System.Data.DataTable -ArgumentList 'User';

    $NewColumn = $DataTable.Columns.Add('Id',[System.Int32]);
    $NewColumn.AllowDBNull = $false;
    $NewColumn.Unique = $true;

    $NewColumn = $DataTable.Columns.Add('FirstName',[System.String]);
    $NewColumn.MaxLength = 64;

    $NewColumn = $DataTable.Columns.Add('MiddleName',[System.String]);
    $NewColumn.MaxLength = 64;

    $NewColumn = $DataTable.Columns.Add('LastName',[System.String]);
    $NewColumn.MaxLength = 64;

    $NewRow = $DataTable.NewRow();
    $NewRow.Id = 1;
    $NewRow.FirstName = 'Test';
    $NewRow.MiddleName = '';
    $NewRow.LastName = 'User';

    $DataTable.Rows.Add($NewRow);

    $NewRow = $DataTable.NewRow();
    $NewRow.Id = 2;
    $NewRow.FirstName = 'Other';
    $NewRow.MiddleName = 'Test';
    $NewRow.LastName = 'User';

    $DataTable.Rows.Add($NewRow);

    return $DataTable;
}
</code></pre>

<p>Nope.  This function returns a <code>[System.Object[]]</code> containing individual <code>[System.Data.DataRow]</code>.  An object array of DataRows.</p>

<p>How can I return a DataTable from a function in PowerShell?</p>