1
votes

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
Try Get-Command -Module sqlserver - Maximilian Burszley
Throw in the code that you are using, it could be something to do with that e.g. Remote querying, etc - Shaneis
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:' - PRS
Those lines match the first 3 lines here but then all the CmdLet's are listed, - crokusek

4 Answers

1
votes

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
0
votes

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

0
votes

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

0
votes

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.