title: Assessing column of a datatable by name instead of column index
body: <p>Is there a way, in powershell, to access the information from a column by its column name instead of its column index?</p>
  
  <p>More specifically, I have the following powershell script:</p>
  
  <pre><code>$SQLQuery = "select * from fruits"
  $server   = $env:COMPUTERNAME
  $Database = "Test"
  $myTable  = "Fruits"
  $Datatable = New-Object System.Data.DataTable
  $Connection = New-Object System.Data.SQLClient.SQLConnection
  $Connection.ConnectionString = "server='$Server';database='$Database';trusted_connection=true;"
  $Connection.Open()
  $Command = New-Object System.Data.SQLClient.SQLCommand
  $Command.Connection = $Connection
  $Command.CommandText = $SQLQuery
  $Reader = $Command.ExecuteReader()
  $Datatable.Load($Reader)
  $Connection.Close()
  
  foreach ($r in $Datatable.rows)
  {
        Write-Host $($r[1])
  }
  </code></pre>
  
  <p>The script prints the data stored in the 2nd column of the table called Fruits ("FruitName") fine, but I wonder if there is a way to assess the 2nd column by referring to FruitName instead?</p>
  
  <p>This is my data structure:</p>
  
  <p><a href="https://i.stack.imgur.com/hgCnl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hgCnl.png" alt="enter image description here"></a></p>