PowerShell – How to execute SQL queries

SqlServer PowerShell module
SqlServer PowerShell module

Hi All,

Greetings for the day!

Learning PowerShell continues.

Today I’ll share PowerShell CMDLETs to execute SQL queries.

UseCase:

  • We have a scenario where we need to read values from SQL Database and create the new post in SharePoint.
  • I went for the PowerShell.
  • To read the values from the SQL database I looked for PowerShell CMDLET.

Details:

  • For connecting to SQL server, we have PowerShell module – SqlServer
  • We need to install the SqlServer PowerShell module
  • Check if SqlServer module is already installed using Get-Module cmdlet

Get-Module -Name SqlServer
  • If above CMDLET returns nothing, means SqlServer PowerShell module is not installed, as shown in below image
PowerShell terminal showing the command 'Get-Module -Name SqlServer' executed by an administrator.
image: PowerShell – SqlServer module – Verifying if SqlServer PowerShell module is installed
  • We will install SqlServer PowerShell module using Install-Module as
Install-Module SqlServer

PowerShell window displaying commands to install the SqlServer module, including an untrusted repository warning.
image: PowerShell – SqlServer module – Installing SqlServer PowerShell module
  • We could verify if SqlServer PowerShell module is successfully installed using Get-Module PowerShell CMDLET as

PS C:\> Get-Module -Listavailable *sql*
Directory: C:\Users\u1086350\Documents\PowerShell\Modules
ModuleType Version PreRelease Name PSEdition ExportedCommands
---------- ------- ---------- ---- --------- ----------------
Script 22.4.5.1 SqlServer Core,Desk {Add-RoleMember, Add-SqlAvailabilityDat…
PS C:\>

PowerShell command output displaying available SQL modules and their details.
image: PowerShell – SqlServer module – Verifying if SqlServer PowerShell module is successfully installed or not

  • Once SqlServer PowerShell module is installed successfully, we are ready to exceute SQL queries
  • We will use “Invoke-Sqlcmd” PowerShell CMDLET to execute the SQL queries as


Invoke-Sqlcmd -Query "SELECT GETDATE() AS TimeOfQuery;" -ServerInstance "<my SQL Server>" -TrustServerCertificate

  TimeOfQuery
-----------
1/31/2026 12:03:20 AM
  

A screenshot of a PowerShell window showing a command execution using Invoke-Sqlcmd to query the current date from a SQL Server instance, displaying the result 'TimeOfQuery' with the date '1/31/2026 12:03:20 AM'.
image: PowerShell – SqlServer module – Executing SQL query using “Invoke-Sqlcmd”

REFERENCES:

Thanks for reading!!! HAVE a FANTASTIC LEARNING AHEAD 🙂

LIFE IS BEAUTIFUL 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL. ENJOY THE WHOLE JOURNEY :) Founder of Microsoft 365 Junction, Speaker, Author, Learner, Developer, Passionate Techie. Certified Professional Workshop Facilitator / Public Speaker. Believe in knowledge sharing. Around 20+ years of total IT experience and 17+ years of experience in SharePoint and Microsoft 365 services Please feel free me to contact for any SharePoint / Microsoft 365 queries. I am also very much interested in behavioral (life changing) sessions like motivational speeches, Success, Goal Setting, About Life, How to live Life etc. My book - Microsoft 365 Power Shell hand book for Administrators and Beginners and 100 Power Shell Interview Questions - https://www.amazon.in/Microsoft-Administrators-Beginners-Interview-Questions/dp/9394901639/ref=tmm_pap_swatch_0?_encoding=UTF8&qid=1679029081&sr=8-11

You may also like...

1 Response

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.