Powershell Load DataTable to Multi Dimensional Array

Jaeden Ruiner 131 Reputation points
2020-11-13T22:04:51.003+00:00

Okay,

THis should be simple, but thus far i have not been able to do this without annoying over-typed for loops and a lot of other hooplah that shouldn't be needed.

        $conn = New-Object System.Data.SqlClient.SqlConnection  
        $conn.ConnectionString = "Server=$($serverName);Integrated Security=true";  
        $conn.Open();  
  
        $cmd = $conn.CreateCommand()  
        $cmd.CommandText = "SELECT Name, Value FROM mydb.dbo.Table";  
        $table = New-Object System.Data.DataTable  
        $table.Load($cmd.ExecuteReader())  
  
        $conn.Close();  
          
        $column = $table.Columns[0].ColumnName  
  
        $data = @($table | select -ExpandProperty $column)  

That above gives me an array of the first column, but i want it to get me an array of the row data. so each row has an "array of values" and i want an array of those arrays.
The C# equivalent would be:

object[][] data = table.OfType<DataRow>().Select(r => r.ItemArray)

or to be more type specific:

string[][] data = table.OfType<DataRow>().Select(r => r.ItemArray.Select(c => c.ToString()).ToArray())

Thus far, i cannot seem to do this is a simple statement or pair of statements in PowerShell, i seem to require doing some form of loop, or create custom objects, etc.
i just want the data formatted into arrays.

Thanks
Jaeden "Sifo-Dyas" al'Raec Ruiner

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2020-11-13T22:46:51.577+00:00

    Try Add-Type to execute C# in PowerShell: executing-c-code-in-powershell


  2. Anonymous
    2020-11-16T09:05:44.977+00:00

    Hi,

    In powershell you can create a two dimensional array in this way

    [string[,]]$data = [string[,]]::new(5,5)  
    

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_arrays?view=powershell-5.1#properties-of-arrays

    Best Regards,
    Ian

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. Anonymous
    2020-11-17T09:52:22.523+00:00

    Hi,

    Sorry for the misunderstanding. You can create an array of arrays like this

    [array[]]$data = [array[]]::new(5)  
    

    Best Regards,
    Ian

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.