Invoke-SqlCmd suddenly stopped working

I’ve been using Visual Code to do all my PowerShell programming lately (instead of the PowerShell ISE). I’ve created several scripts that use Invoke-SqlCmd and they’ve all worked fine in VS Code until today.

A simple test like:

Invoke-Sqlcmd -Query 'SELECT @@VERSION' -ConnectionString 'Data Source=myserver;Initial Catalog=master;Integrated Security=True;'

… now gives this error when run in Visual Code:

Invoke-Sqlcmd : Could not load file or assembly ‘Microsoft.SqlServer.BatchParser, Version=15.100.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91’ or one of its dependencies. The system cannot find the file
specified.

The exact same code, when run in the PowerShell ISE on the same machine, runs fine. Both the ISE and VS Code are running PS 5.1.

Get-Module -Name "SqlServer"

… in both PS ISE and VS Code shows:

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     21.1.18256 SqlServer                           {Add-RoleMember, Add-SqlAvailabilityDatabase, Add-SqlAvailabilityGroupListenerStaticIp, Add-SqlAzureAuthenticationContext...}

I’m at a loss as to how to get this working in VS Code again. Last week, I installed Visual Studio 2022 on this computer, and I wonder if that changed something that broke VS Code.

Any suggestions?

UnInstall Visual Studio 2022
ReInstall VSCode and all extensions and modules you’re using.

1 Like

I eventually figured it out - Visual Studio Code was running the 32-bit version of PowerShell 5.1. Once I told it to use the 64-bit version, Invoke-SqlCmd worked properly.

1 Like