dbatools is a free PowerShell module with over 300 SQL Server administration, best practice and migration commands included.

Please note that documentation and command names may be out of date while we work furiously towards 1.0


This function is a wrapper command around Invoke-SqlCmd2.
It was designed to be more convenient to use in a pipeline and to behave in a way consistent with the rest of our functions.




Runs the sql query ‘SELECT foo FROM bar’ against the instance ‘server\instance’.

Invoke-DbaSqlCmd -SqlInstance server\instance -Query ‘SELECT foo FROM bar’

Runs the sql query ‘SELECT foo FROM bar’ against all instances in the group [GROUPNAME] on the CMS [SERVERNAME].

Get-DbaRegisteredServer -SqlInstance [SERVERNAME] -Group [GROUPNAME] | Invoke-DbaSqlCmd -Query ‘SELECT foo FROM bar’

Runs the sql commands stored in rebuild.sql against the instances “server1”, “server1\nordwind” and “server2”.

“server1”, “server1\nordwind”, “server2” | Invoke-DbaSqlCmd -File “C:\scripts\sql\rebuild.sql”


This command was created by Fred Winmann. You can find Fred on Twitter.


From PowerShell, execute Get-Help Invoke-DbaSqlCmd -Detailed for more information on this function.

Source Code

Want to see the source code? View Invoke-DbaSqlCmd.ps1 on GitHub

Related commands