-1

I'm encountering an error when trying to create a database and also to execute other queries in SQL Server using PowerShell's Invoke-SqlCmd command.

Environment:

  • Windows Version: 10.0.19045
  • PowerShell Version: 5.1.19041.4780

Powershell command used:

Invoke-SqlCmd -ServerInstance "localhost\sqlexpress" 
              -Query "CREATE DATABASE dummy_db"

Error:

Invoke-SqlCmd : The 'Invoke-SqlCmd' command was found in the module 'SQLPS', but the module could not be loaded. For more information, run 'Import-Module SQLPS'.

At line:1 char:1

  • Invoke-SqlCmd -ServerInstance "localhost\sqlexpress" -Query "CREATE D ...
  • CategoryInfo : ObjectNotFound: (Invoke-SqlCmd:String) [], CommandNotFoundException
  • FullyQualifiedErrorId : CouldNotAutoloadMatchingModule

Outside Powershell:

  • I can successfully execute queries directly from the command prompt using sqlcmd.
  • I can also manage the database using third-party tools like HeidiSQL with no issues.
  • I attempted to troubleshoot using a similar Stack Overflow thread (URL: Powershell Invoke-SQLCmd and SQLPS) but haven't found a solution.

What additional troubleshooting steps can I take to resolve this error? Is there any step I missed?

10
  • So what happened when you followed the error's instructions? What about that information didn't you understand? What was that information? Commented Sep 5, 2024 at 14:24
  • I have followed the directions from a similar thread, by installing the sqlserver module in powershell, it says it is already there. Command: Install-module -Name SqlServer -Scope CurrentUser. Result: PackageManagement\Install-Package : The following commands are already available on this system:'Add-SqlAvailabilityDat abase,Add-SqlAvailabilityGroupListenerStaticIp, etc Commented Sep 5, 2024 at 14:34
  • But what about the instructions in the error...? You haven't told us what happened when you followed the error's instructions. It might seem like a "silly" question but; have you read the error message? What about the error didn't you understand and the instructions it gave you? Commented Sep 5, 2024 at 14:34
  • And also check for the existence of sqlps using command: Get-Module -ListAvailable -Name Sqlps. ModuleType Version Name Manifest 15.0 SQLPS Commented Sep 5, 2024 at 14:36
  • 1
    Please do read your error messages before posting on Stack Overflow; this site is for when you and tried and failed to solve the problem, not to post your question when you hit the first bump in road. If the error gives you instructions, follow them; they are informative for a reason. Commented Sep 5, 2024 at 14:42

1 Answer 1

0

As explained in the comments, this error occurs because the PowerShell execution policy is set to a restricted mode, preventing the execution of scripts. So I tried changing the execution policy with the command: Set-ExecutionPolicy RemoteSigned -Scope CurrentUser, and finally the Invoke-SqlCmd command could be executed successfully.

Thanks

DeeKey

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

2 Comments

It would be helpful if the error message from SQLPS were more helpful. Have you tried getting current on PowerShell Core 7 and the SqlServer module instead of SQLPS?
Hi lit, I haven't tried it yet, maybe later if I want to dig deeper. But thanks for the insight.

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.