Using Powershell to make MS SQL query

Making SQL query is pretty simple, hope the following give you a quickest way to get sql data without using SQL Management Tool. (Note: Windows 7 does come with Powershell 2.0, Hurray!)

[sourcecode language=”powershell”]

$sql = New-Object System.Data.SqlClient.SqlConnection
$sql.ConnectionString = "Server=localhostsqlexpress;Integrated Security=true;"
$sql.Open()
$cmd = $sql.CreateCommand()
#Here is where you insert your own sql query
$cmd.CommandText = "exec sp_databases"
$cmd.ExecuteScalar()

[/sourcecode]

Leave a Reply

Your email address will not be published. Required fields are marked *