SQL Select with Powershell on MSSQL
Video walkthrough (ran against a live database): https://youtu.be/VZycEzeXKXs
Below are the commands used in the video, available here so you can easily read through and adapt them to your needs.
$SqlQuery = @"
SELECT *
FROM TB_TEST
"@
$DataBaseName = "TESTING"
$SQLConnection = New-Object System.Data.SqlClient.SqlConnection
$SQLConnection.ConnectionString="Server=localhost;Database=$DataBaseName;Integrated Security=SSPI;"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$sqlCmd.Connection = $SQLConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$DataSet.Tables[0]