Select-Object of multiple properties

Viewed 32465

I am trying to find an elegant way to put the metadata of a table of type System.Data.DataTable into a multi-dimensional array for easy reference in my program. My approach to the issue so far seems tedious.

Assuming $DataTable being the DataTable in question

What I tried to do so far was:

$Types = $DataTable.Columns | Select-Object -Property DataType
$Columns= $DataTable.Columns | Select-Object -Property ColumnName
$Index = $DataTable.Columns | Select-Object -Property ordinal
$AllowNull  = $DataTable.Columns | Select-Object -Property AllowDbNull

Then painfully going through each array, pick up individual items and put them in my multi-dimensional array $TableMetaData.

I read in the documentation of Select-Object and it seems to me that only 1 property can be selected at 1 time? I think I should be able to do all the above more elegantly and store the information in $TableMetaData.

Is there a way to easily pick up multiple properties and put them in a multi-dimensional array in 1 swoop?

2 Answers

One easy way to do this is to create an "empty" variable with Select-Object. Here is a sample command:

$DataTableReport = "" | Select-Object -Property DataType, ColumnName, ordinal,  AllowDbNull 

Then, link the $DataTableReport to the $Types, $Columns, $Index, and the $AllowNull properties as shown below:

$DataTableReport.Types = $DataTable.DataType
$DataTableReport.Columns = $DataTable.ColumnName 
$DataTableReport.Index = $DataTable.ordinal 
$DataTableReport.AllowNull  = $DataTable.AllowDbNull 

Finally, call the DataTableReport variable.

$DataTableReport # will display all the results in a tabular form.

Related