Query SQL with PowerShell

Image Description

Daily PowerShell #42

Daily PowerShell SQL

November 27, 2021

quote Discuss this Article

In this post, we’ll query SQL with PowerShell.

Query SQL with DBATools

DBATools is an amazing PowerShell module that anyone dealing with SQL should be aware of. The Invoke-DbaQuery cmdlet can be used to query tables.

Install-Module dbatools
Invoke-DbaQuery -Query "SELECT * FROM job" -SqlInstance '(localdb)\MSSQLLocalDB' -Database universal

Query SQL with SqlServer

The SQLServer module is the official SQL module developed by Microsoft. You can use the Invoke-SqlCmd cmdlet to query SQL using this module.

Install-Module SqlServer
Invoke-SqlCmd -ServerInstance '(localdb)\MSSQLLocalDb' -Database universal -Query 'SELECT * FROM job'

Query SQL with InvokeQuery

InvokeQuery is a popular community module for querying various types of databases. It supports databases like Sqlite, MS SQL and MySQL.

Install-Module InvokeQuery
Invoke-SqlServerQuery -Database universal -Server '(localdb)\MSSQLLocalDB' -Sql 'SELECT * FROM job'