1

I am trying to learn Sqlcmd utility in PowerShell to run SQL queries, specifically for database administration.

Using Version 15.0.1300.359 NT

But I got confused, what is the difference between Sqlcmd and Invoke-Sqlcmd?

Why is it this command works:

Invoke-Sqlcmd -ServerInstance myinstance -Database AdventureWorks2017 "select top 3 * from [Person].[BusinessEntity]"

And this one does not work:

Invoke-Sqlcmd -S myinstance -d AdventureWorks2017 "select top 3 * from [Person].[BusinessEntity]"

By querying help I can clearly see that I should be able using all those variables:

enter image description here

2 Answers 2

5

If you're working on PowerShell scripting, I would recommend the use of the Module whenever possible. Invoke-SqlCmd will output objects, which are definitely easier to manipulate.

Sign up to request clarification or add additional context in comments.

Comments

0

-S is ambiguous in invoke-sqlcmd, use -ServerInstance when referencing your server/instance. SQLCMD uses a different set of parameters/options than Invoke-Sqlcmd and most abbreviations from sqlcmd cannot be used. Use the set as described in the Invoke-Sqlcmd reference from Microsoft: https://learn.microsoft.com/en-us/powershell/module/sqlserver/invoke-sqlcmd?view=sqlserver-ps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.