PowerShell – How to execute SQL queries

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

- We will install SqlServer PowerShell module using Install-Module as
Install-Module SqlServer

- 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\ModulesModuleType Version PreRelease Name PSEdition ExportedCommands---------- ------- ---------- ---- --------- ----------------Script 22.4.5.1 SqlServer Core,Desk {Add-RoleMember, Add-SqlAvailabilityDat…PS C:\>

- 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

REFERENCES:
- Install SQL Server PowerShell module – https://learn.microsoft.com/en-us/powershell/sql-server/download-sql-server-ps-module?view=sqlserver-ps
- Invoke-Sqlcmd – https://learn.microsoft.com/en-us/powershell/module/sqlserver/invoke-sqlcmd?view=sqlserver-ps
Thanks for reading!!! HAVE a FANTASTIC LEARNING AHEAD 🙂
LIFE IS BEAUTIFUL 🙂

1 Response
[…] We have an article – PowerShell : How to execute SQL queries – https://knowledge-junction.in/2026/01/31/powershell-how-to-execute-sql-queries/ […]