I've setup a simple sqlcmd script. Contents below.

  use $(dbname);
  select * from $(tablename)

I'm then running the following Invoke SQLCMD

$SQLCMDVarArr = "dbname='MyDatabase'", "Tablename='MyTable'"

INVOKE-SQLCMD -ServerInstance MyServer\MyInstance -inputFile '\\MyShare\Database\Sprints\csrtemp2\csrtest.sql' -variable $SQLCMDVarArr

And I'm getting the error:

  INVOKE-SQLCMD : Incorrect syntax near 'MyDatabase'.

I pulled the syntax straight from the invoke-sqlcmd get-help examples. Can anyone tell me what is wrong with this?


as per request this is running on windows Server 2008 R2

  • Could you please add which Operating System you are running the script on? It could be relevant seeing as Powershell is now available for Linux. Installing Windows PowerShell
    – John K. N.
    Commented Dec 13, 2017 at 14:09
  • @hot2use - added the requested information (windows server 2008r2)
    – Lumpy
    Commented Dec 13, 2017 at 14:12
  • 1
    @hot2use While PowerShell is available on Linux, SQL Server PowerShell tools are not, they do not run nor install. Just FYI.
    – user507
    Commented Dec 13, 2017 at 16:29

1 Answer 1


So it turns out that the get-help invoke-sqlcmd -example shows the array variable as using 'dbname="MyDatabase"' as the syntax. However this gives the error above but if you swap the quote positions such that the variable uses "dbname='MyDatabase'" then the script works.

  • 1
    Online version of the help seems to be showing correctly, here. The version that would be with the release of SQL Server will not get these updates. So just future reference good idea to check online version for some of those commands as they tend to show more accurate info.
    – user507
    Commented Dec 13, 2017 at 16:31

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.