In the previous articles, I have shared basic about the SQL Server PowerShell (sqlps) and how we can navigate SQL Server instance data using PowerShell.
In the SQLPS module, there are different commands available to extensively work with SQL Server.
The cmdlet command type contains most of the commands of SQLPS.
The Invoke-SQLCMD is also one command to execute T-SQL in PowerShell.
Here, I am sharing some of the examples of Invoke-SQLCMD to execute T-SQL in PowerShell.
First, list of all commands of SQLPS:
Get-command –module SQLPS
Check SQL Server Version:
Invoke-SQLCMD "select @@version;"
Check Default database name:
Invoke-SQLCMD "select db_name();"
Execute SELECT statement:
Invoke-SQLCMD -Query "SELECT *FROM dbo.tbl_TestSnapshot;" –Database "dbrnd"
Execute SELECT with PowerShell Variable:
$MyVar = "Anvesh"
Invoke-SQLCMD -Query "SELECT *FROM dbo.tbl_TestSnapshot WHERE Name = '$($MyVar)';" –Database "dbrnd"
Please share your ideas and opinions about this topic with me, your contribution will add true value to this topic.
If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.
If you like this post, then please share it with others.
Please follow dbrnd.com, I will share my experience towards the success of Database Research and Development Activity.
I put up a post every day, please keep reading and learning.
Discover Yourself, Happy Blogging !
Anvesh M. Patel.