# Call operator doesn't work when sqlcmd command promt aranged as variable

**URL:** <https://forums.powershell.org/t/call-operator-doesnt-work-when-sqlcmd-command-promt-aranged-as-variable/12598>\
**Category:** PowerShell Help\
**Created:** [May 21, 2019, 2:48pm UTC](https://forums.powershell.org/t/call-operator-doesnt-work-when-sqlcmd-command-promt-aranged-as-variable/12598 "2019-05-21T14:48:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![andy](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@andy](https://forums.powershell.org/u/andy)\
**Post date:** [May 21, 2019, 2:48pm UTC](https://forums.powershell.org/t/call-operator-doesnt-work-when-sqlcmd-command-promt-aranged-as-variable/12598/1 "2019-05-21T14:48:02Z")

</div>

Hello, All  
I need to execute several “sqlcmd” command prompt in my PowerShell script. When I execute this command directly in powershell using ‘&’ operator - all works fine:  
& sqlcmd -S SQLSRV -v DatabaseName=“MyDb” -i “.\MyScript.sql” -b

but, if I arrange ‘sqlcmd…’ to PowerShell variable - I have got error:  
$sqlcommand = “sqlcmd -S SQLSRV -v DatabaseName=`"MyDb`” -i `".\MyScript.sql`" -b"

& $sqlcommand  
& : The term ‘sqlcmd -S SQLSRV -v DatabaseName=“MyDb” -i “.\MyScript.sql” -b’ is not recognized as the name of a cmdlet, function, script file, or operable program.  
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.  
How can I execute this “sqlcmd” command through variable ? Or may be make sense to use “Invoke-Expression” instead of “&” ?  
Btw:  
Invoke-Expression $sqlcommand works properly.

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [May 21, 2019, 3:33pm UTC](https://forums.powershell.org/t/call-operator-doesnt-work-when-sqlcmd-command-promt-aranged-as-variable/12598/2 "2019-05-21T15:33:48Z")

</div>

Why not just do

```
sqlcmd -S SQLSRV -v DatabaseName="MyDb" -i ".\MyScript.sql" -b
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [May 21, 2019, 4:07pm UTC](https://forums.powershell.org/t/call-operator-doesnt-work-when-sqlcmd-command-promt-aranged-as-variable/12598/3 "2019-05-21T16:07:10Z")

</div>

Why can’t you just use the Invoke-Sqlcmd cmdlet?

[https://docs.microsoft.com/en-us/powershell/module/sqlserver/invoke-sqlcmd?view=sqlserver-ps](https://docs.microsoft.com/en-us/powershell/module/sqlserver/invoke-sqlcmd?view=sqlserver-ps)

&nbsp;

---

<div class="post-metadata">

**Author:** ![rob\_cliffeild](https://avatars.discourse-cdn.com/v4/letter/r/a9a28c/32.png) [@rob\_cliffeild](https://forums.powershell.org/u/rob_cliffeild)\
**Post date:** [May 21, 2019, 4:14pm UTC](https://forums.powershell.org/t/call-operator-doesnt-work-when-sqlcmd-command-promt-aranged-as-variable/12598/4 "2019-05-21T16:14:49Z")

</div>

Running SQLCMD directly works as JS mentioned.

For a full reference to how to run an executable have a look at this Microsoft article:  
[https://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx](https://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx)

---

<div class="post-metadata">

**Author:** ![rob\_cliffeild](https://avatars.discourse-cdn.com/v4/letter/r/a9a28c/32.png) [@rob\_cliffeild](https://forums.powershell.org/u/rob_cliffeild)\
**Post date:** [May 21, 2019, 4:29pm UTC](https://forums.powershell.org/t/call-operator-doesnt-work-when-sqlcmd-command-promt-aranged-as-variable/12598/5 "2019-05-21T16:29:08Z")

</div>

[quote quote=157733]Why can’t you just use the Invoke-Sqlcmd cmdlet?

[https://docs.microsoft.com/en-us/powershell/module/sqlserver/invoke-sqlcmd?view=sqlserver-ps](https://docs.microsoft.com/en-us/powershell/module/sqlserver/invoke-sqlcmd?view=sqlserver-ps)

[/quote]  
This is the native PowerShell and has limitations if you have SQL Server Manager installed I would recommend using SQLCMD and a prebuild SQL query.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [May 21, 2019, 5:15pm UTC](https://forums.powershell.org/t/call-operator-doesnt-work-when-sqlcmd-command-promt-aranged-as-variable/12598/6 "2019-05-21T17:15:25Z")

</div>

Limitations in simply running an .sql query?

I’ve done it, seen no limitations on my end.

&nbsp;

---

<div class="post-metadata">

**Author:** ![rob\_cliffeild](https://avatars.discourse-cdn.com/v4/letter/r/a9a28c/32.png) [@rob\_cliffeild](https://forums.powershell.org/u/rob_cliffeild)\
**Post date:** [May 21, 2019, 5:31pm UTC](https://forums.powershell.org/t/call-operator-doesnt-work-when-sqlcmd-command-promt-aranged-as-variable/12598/7 "2019-05-21T17:31:33Z")

</div>

Last time I used it I had a case statement and a minimum of 3 different joins, there was no chance of it working. I haven’t tested it in the latest version as the SQLCMD method isn’t broken for me so I am not looking for a fix.

Solution was to install SQL Management Studio and use SQLCMD.

If it works now then great I might go back to using native powershell and cull the use of sqlcmd

---

<div class="post-metadata">

**Author:** ![andy](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@andy](https://forums.powershell.org/u/andy)\
**Post date:** [May 21, 2019, 11:42pm UTC](https://forums.powershell.org/t/call-operator-doesnt-work-when-sqlcmd-command-promt-aranged-as-variable/12598/8 "2019-05-21T23:42:37Z")

</div>

[quote quote=157733]Why can’t you just use the Invoke-Sqlcmd cmdlet?

[https://docs.microsoft.com/en-us/powershell/module/sqlserver/invoke-sqlcmd?view=sqlserver-ps](https://docs.microsoft.com/en-us/powershell/module/sqlserver/invoke-sqlcmd?view=sqlserver-ps)

[/quote]  
Because Invoke-Sqlcmd doesn’t work if you have “==” in your db password:  
[sql server - Escape variable in sqlcmd / Invoke-SqlCmd - Stack Overflow](https://stackoverflow.com/questions/35157090/escape-variable-in-sqlcmd-invoke-sqlcmd)

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:32pm UTC](https://forums.powershell.org/t/call-operator-doesnt-work-when-sqlcmd-command-promt-aranged-as-variable/12598/9 "2024-05-16T20:32:45Z")

</div>

Closed on May 16, 2024, 8:32pm UTC


