Getting SQL Server result set in PowerShell

Compatibility: v5.x
If you are a .NET developer then you are already familiar with this code Smile


$connectionString = "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=AdventureWorks2016CTP3"
$dt = New-Object System.Data.DataTable
$da = New-Object System.Data.SqlClient.SqlDataAdapter("SELECT TOP 10 * FROM Person.Address", $connectionString)
$da.Fill($dt)

Write-Output $dt.Rows[0]["AddressLine1"]

# or

ForEach ($dr in $dt.Rows) {
Write-Output $dr["AddressLine1"]
}