Power$nippets

Boost your shell with reusable code stripped to its essentials

ConvertTo-DataTable

Convert an object (list) to a data table.

Syntax

<Object> | ConvertTo-DataTable [-ColumnType] <HashTable>

Parameters

[-Object] <Object>
The object(s) to be converted to a DataTable.

[-ColumnType] <HashTable>
Type definition of each column by name: @{<ColumnName> = <Type>; ...}

Examples

Converting a custom object to a data table

$MyObject = @(
    New-Object PSCustomObject -Property @{Name = "a"; Value = 123; Date = $Null}
    New-Object PSCustomObject -Property @{Name = "b"; Value = 123456789012; Date = $Now}
)
$DataTable = $MyObject | ConvertTo-DataTable

Defining the column types:

$DataTable = $MyObject | ConvertTo-DataTable -ColumnType @{Name = [String]; Value = [Int64]; Date = [DateTime]}

Convert (PowerShell) process object

$DataTable = Get-Process 'PowerShell' | ConvertTo-DataTable

Convert User Language List

$DataTable = Get-WinUserLanguageList | ConvertTo-DataTable
Last updated on 29 Jun 2020
Published on 6 Nov 2017