Skip to content

Extend [DataTable] with easy constructor and AddRow method #11987

Closed

Description

@iRon7

A list of [PSCustomObject] objects consumes multiple times the amount of memory as a [DataTable]. For a lot of .Net classes there are accelerators and PowerShell constructors but to create a [DataTable] in PowerShell is quiet an hassle, see e.g. Creating table using Powershell.

It would be nice to be able to construct a data table the PowerShell way, e.g.:

$Table = [DataTable]('Text', 'Number')

Or:

$Table = [DataTable]@{Text = [String]; 'Number' = [Int]}

And add data rows like:

$Table.AddRow(@('A', 1))

Or:

$Table.AddRow(@{Text = 'A',  Number = 1})

As I don't have enough experience with C#, I have created a derived [DataTable] class in
PowerShell to test the idea:

class DataTable : Data.DataTable {
	DataTable([Array]$ColumnNames) {
		ForEach ($ColumnName in $ColumnNames) { 
			If ($ColumnName -is [PSCustomObject]) {Write-Host 'PSCustomObject'}
			Else { $This.AddColumn($ColumnName) }
		}
	}
	DataTable([System.Collections.IDictionary]$ColumnNameTypes) {
		ForEach($Key in $ColumnNameTypes.Get_keys()) { $This.AddColumn($Key, $ColumnNameTypes[$Key]) }
	}
	[Void]AddColumn([String]$ColumnName) {
		$DataColumn = New-Object Data.DataColumn($ColumnName)
		$This.Columns.Add($DataColumn)
	}
	[Void]AddColumn([String]$ColumnName, [Type]$ColumnType = [String]) {
		$DataColumn = New-Object Data.DataColumn($ColumnName, $ColumnType)
		$This.Columns.Add($DataColumn)
	}
	[Void]AddRow([Array]$Array) {
		$DataRow = $This.NewRow()
		For ($i = 0; $i -lt $Array.Length; $i++) {
			If ($i -lt $This.Columns.Count) { $DataRow.Item($i) = $Array[$i] }
		}
		$This.Rows.Add($DataRow)
	}
	[Void]AddRow([System.Collections.IDictionary]$Dictionary) {
		$DataRow = $This.NewRow()
		ForEach ($Name in $Dictionary.get_Keys()) {
			If ($This.Columns.Contains($Name)) { $DataRow.Item($Name) = $Dictionary[$Name] }
		}
		$This.Rows.Add($DataRow)
	}
	[Void]AddRow([PSCustomObject]$PSObject) {
		$DataRow = $This.NewRow()
		ForEach ($Name in $PSObject.PSObject.Properties.Name) {
			If ($This.Columns.Contains($Name)) { $DataRow.Item($Name) = $PSObject.$Name }
		}
		$This.Rows.Add($DataRow)
	}
}

#Example 1

$MyDataTable = [DataTable]('a', 'b', 'c')
$MyDataTable.AddRow(@(1,2,3))
$MyDataTable

a b c
- - -
1 2 3

#Example 2

$Table = [DataTable]@{Text = [String]; 'Number' = [Int]}
$Table.AddRow(@{Text = 'A'; Number = 1})
$Table

Text Number
---- ------
A         1

# Example 3

# $Process = [DataTable]@((Get-Process | Select-Object -First 1).PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames)
$Process = [DataTable][Ordered]@{Id = [Int]; ProcessName = [String]}
Get-Process | ForEach-Object { $Process.AddRow($_) }
$Process

Activity

  1. added
    Issue-Enhancement
    on Mar 2, 2020
  2. changed the title [-]Accelerated [DataTable] with easy constructor and AddRow method[/-] [+]Extend [DataTable] with easy constructor and AddRow method[/+] on Mar 2, 2020
  3. added
    WG-Engine
    on Mar 2, 2020
  4. iSazonov commented on Mar 2, 2020

    @iSazonov
    Collaborator

    Also you could look TypedTableBase<T> class.

  5. added
    WG-Language
    on Oct 29, 2022
  6. SteveL-MSFT commented on Oct 29, 2022

    @SteveL-MSFT
    Member

    It seems like this is something that could be packaged as a module and published to PowerShellGallery. Maybe New-DataTable and ConvertTo-DataTable cmdlets.

  7. added
    Resolution-No Activity
    on Nov 16, 2023
  8. iRon7 commented on Nov 16, 2023

    @iRon7
    Author

    Please either mark this issue with something like Resolution-Declined or put it on a kind of wishlist "Issue we would like to prioritize, but we can't commit we will get to it yet

  9. removed
    Resolution-No Activity
    on Nov 16, 2023
  10. added
    Resolution-No Activity
    on May 14, 2024
  11. iRon7 commented on May 15, 2024

    @iRon7
    Author

    Please either mark this issue with something like Resolution-Declined or put it on a kind of wishlist "Issue we would like to prioritize, but we can't commit we will get to it yet

  12. removed
    Resolution-No Activity
    on May 15, 2024
  13. added
    Needs-Triage
    WG-NeedsReview
    and removed
    WG-Language
    on May 15, 2024
  14. SeeminglyScience commented on Jun 2, 2025

    @SeeminglyScience
    Contributor

    The Engine WG discussed this, we do not feel this meets the bar for impact and suggest implementing as a module.

  15. added
    Resolution-Declined
    and removed
    Needs-Triage
    WG-NeedsReview
    on Jun 2, 2025
  16. microsoft-github-policy-service commented on Jun 3, 2025

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as declined and has not had any activity for 1 day. It has been closed for housekeeping purposes.

  17. microsoft-github-policy-service commented on Jun 3, 2025

    @microsoft-github-policy-service
    Contributor

    📣 Hey @iRon7, how did we do? We would love to hear your feedback with the link below! 🗣️

    🔗 https://aka.ms/PSRepoFeedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-EnhancementResolution-DeclinedWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions