2

When I try to run a powershell script I get the following error:

Invoke-Sqlcmd : The term 'Invoke-Sqlcmd' is not recognized as the name of a cmdlet, function, script file, or operable program.

So in Powershell I ran the following:

install-module sqlserver
update-module sqlserver
import-module sqlserver

This all runs fine. However when I rerun my script I still get the same error.

I have also installed powershelltools.msi, downloaded as part of the SQL Server 2014 Feature pack here: https://www.microsoft.com/en-gb/download/details.aspx?id=42295

If I run this command:

Get-Command -Module sqlserver

I get this result:

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           Decode-SqlName                                     21.0.17224 sqlserver
Alias           Encode-SqlName                                     21.0.17224 sqlserver
Function        SQLSERVER:                                         21.0.17224 sqlserver

Any ideas on what else I should try?

I have SQLServer 2014 and Powershell Version 5

4
  • 1
    Try Get-Command -Module sqlserver Commented Feb 5, 2018 at 12:51
  • Throw in the code that you are using, it could be something to do with that e.g. Remote querying, etc Commented Feb 5, 2018 at 12:56
  • If I do 'Import-module sqlserver', then 'Invoke-Sqlcmd' immediately after that in the same session, I get the same error. 'Get-Command -Module sqlserver' returns a list of 2 aliases 'Decode-sqlName', and 'Encode-sqlName', and a function 'SQLSERVER:' Commented Feb 5, 2018 at 13:30
  • Those lines match the first 3 lines here but then all the CmdLet's are listed, Commented Oct 17, 2018 at 22:04

4 Answers 4

2

Are you using Powershell Core (v6.1)?

I was having a similar issue and found this SO answer. Turns out the SQLServer module for Powershell Core doesn't include the Invoke-SqlCmd (among others). I switched back to the 64-bit version of Powershell I have installed on Windows 10 (v5.1) and installed, then imported the sqlserver module. Invoke-SQLCmd is now listed.

Install-Module -Name SqlServer -AllowClobber
Import-Module -Name SqlServer -Force
Get-Command -Module SqlServer
Sign up to request clarification or add additional context in comments.

2 Comments

FYI For those with 7.2 having this issue, I used just the install-module. I was unable to use invoke-sqlcmd until after I restarted VSCODE.
PowerShell Core now has the Invoke-Sqlcmd command according to this SO answer.
0

Import-Module imports a module only to current powershell session, not globally. Add the import to your script or to profile.

2 Comments

If I do 'Import-module sqlserver', then 'Invoke-Sqlcmd' after that in the same session, I get the same error.
Try Import-Module SQLPS
0

Was searching a solution for the same problem and found the below worked for me.

find-module sqlserver | Install-Module -AllowClobber -Force

Original Answer: https://social.technet.microsoft.com/Forums/en-US/f3a52235-e62a-402e-9b1b-0b0c0cdd17aa/sql-powershell-error-invokesqlcmd-the-term-invokesqlcmd-is-not-recognized-as-the-name-of-a?forum=winserverpowershell

Comments

0

I had the same problem. Apparently I had to unblock all the dll files in the new module folder. In my case C:\Program Files\WindowsPowerShell\Modules\SqlServer. I found the link here how to do it. https://www.404techsupport.com/2016/06/24/unblock-files-powershell/

dir -Path [directory path] -Recurse | Unblock-File

Close powershell if you still have a session open.

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.