I would say that there is too much effort to write a user-format file to force PowerShell to display objects in a table.
If you really want to try, you are already on the right track.
I came up with a simple format definition that displays DataRows in a table format.
However, there is one caveat: I do not think it is possible to have custom labels for each column (you will need to check the schema definition again: https://msdn.microsoft.com/en-us/library/gg580910(v=vs.85 ) .aspx ). In my example, I just use common names such as "Field1", "Field2", etc.
Here is the format definition for 2 columns:
$TableFormat = @' <Configuration> <ViewDefinitions> <View> <Name>MyTable</Name> <ViewSelectedBy> <TypeName>System.Data.DataRow</TypeName> </ViewSelectedBy> <TableControl> <TableHeaders> <TableColumnHeader> <Label>Field 1</Label> <Width>20</Width> </TableColumnHeader> <TableColumnHeader> <Label>Field 2</Label> <Width>20</Width> </TableColumnHeader> </TableHeaders> <TableRowEntries> <TableRowEntry> <TableColumnItems> <TableColumnItem> <ScriptBlock>$_.Item(0)</ScriptBlock> </TableColumnItem> <TableColumnItem> <ScriptBlock>$_.Item(1)</ScriptBlock> </TableColumnItem> </TableColumnItems> </TableRowEntry> </TableRowEntries> </TableControl> </View> </ViewDefinitions> </Configuration> '@
Format Definition Update:
$FormatPath = Join-Path -Path $PSScriptRoot -ChildPath "DataTable.format.ps1xml" $TableFormat | Set-Content -Path $FormatPath -Encoding Default Update-FormatData -Appendpath $FormatPath
And an example output:
$CnStr = "Data Source=Starbase1\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI" $Sql = "Select LastName, City From Employees Where City <> 'Seattle'" $Da = New-Object -TypeName System.Data.SqlClient.SqlDataAdapter -ArgumentList $Sql, $CnStr $Ta = New-Object -TypeName System.Data.DataTable $Da.Fill($Ta) $Ta
source share