Repository navigation
Closed
Labels
Issue-Enhancementthe issue is more of a feature request than a bugResolution-DeclinedThe proposed feature is declined.WG-Enginecore PowerShell engine, interpreter, and runtime
Description
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($_) }
$ProcessReacted by Christopher Bues, Matthew Gray, Scott Hardwick and wqnvlz
Reacted by Christopher Bues
Metadata
Metadata
Assignees
Labels
Issue-Enhancementthe issue is more of a feature request than a bugResolution-DeclinedThe proposed feature is declined.WG-Enginecore PowerShell engine, interpreter, and runtime
Activity
[-]Accelerated [DataTable] with easy constructor and AddRow method[/-][+]Extend [DataTable] with easy constructor and AddRow method[/+]iSazonov commentedon Mar 2, 2020
Also you could look TypedTableBase<T> class.
SteveL-MSFT commentedon Oct 29, 2022
It seems like this is something that could be packaged as a module and published to PowerShellGallery. Maybe
New-DataTableandConvertTo-DataTablecmdlets.iRon7 commentedon Nov 16, 2023
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
iRon7 commentedon May 15, 2024
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
SeeminglyScience commentedon Jun 2, 2025
The Engine WG discussed this, we do not feel this meets the bar for impact and suggest implementing as a module.
microsoft-github-policy-service commentedon Jun 3, 2025
This issue has been marked as declined and has not had any activity for 1 day. It has been closed for housekeeping purposes.
microsoft-github-policy-service commentedon Jun 3, 2025
📣 Hey @iRon7, how did we do? We would love to hear your feedback with the link below! 🗣️
🔗 https://aka.ms/PSRepoFeedback