Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
function New-DataTable {
Create a new DataTable.
Create a new DataTable.
$myColumns = [ordered] @{
ID = [int32]
Name = [string]
Age = [int]
Date = [DateTime]
$myTable = New-DataTable -TableName 'MyTable' -Columns $myColumns
Author: Øyvind Kallstad
Date: 20.10.2014
Version: 1.0
param (
[string] $TableName,
# Use an ordered dictionary to define the table columns.
[System.Collections.Specialized.OrderedDictionary] $Columns
# create new datatable
$newDataTable = New-Object -TypeName 'System.Data.DataTable' -ArgumentList $TableName
Write-Verbose "DataTable created '$TableName'"
# create columns
if ($PSBoundParameters['Columns']) {
$Columns.GetEnumerator() | ForEach-Object {
$columnName = $_.Key
$columnType = $_.Value.ToString()
$dataColumn = New-Object -TypeName 'System.Data.DataColumn' -ArgumentList ($columnName,$columnType)
Write-Verbose "New column created '$columnName'"
Write-Output @(,$newDataTable)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment