1

If the Powershell module is installed and can be found. Why is the cmdlet not being recognized?

PS C:\> Get-SqlInstance -Credential laptop-ql9k5dk6\david -ServerInstance "(localdb)\MSSQLLocalDB"
Get-SqlInstance: The term 'Get-SqlInstance' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

PS C:\> Get-InstalledModule -Name SqlServer -AllVersions | select Name,Version

Name      Version
----      -------
SqlServer 21.1.18256

I don't know the location of the Powershell SqlServer module. But the env paths have worked for everything I have used so far:

$env:PSModulePath -split ";"
C:\Users\david\OneDrive\Documents\PowerShell\Modules
C:\Program Files\PowerShell\Modules
c:\program files\powershell\7\Modules
C:\Users\david\Documents\WindowsPowerShell\Modules
C:\Users\david\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\PowerShell
C:\Program Files\WindowsPowerShell\Modules
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
C:\Program Files (x86)\Microsoft SQL Server\150\Tools\PowerShell\Modules\
c:\Users\david\.vscode\extensions\ms-vscode.powershell-2023.1.0\modules
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Dave
  • 687
  • 7
  • 15
  • I found this recent question which is similar https://stackoverflow.com/questions/73702073/getting-mssql-instance-version-in-powershell – Dave Feb 20 '23 at 02:50

1 Answers1

0

This issue, not recognizing cmdlets, occurs when using the Visual Studio Code (Powershell Extension v2023.1.0) terminal. But NOT when using a separate PS terminal. Both are using the same PS version:

PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.3.2
PSEdition                      Core 
GitCommitId                    7.3.2
OS                             Microsoft Windows 10.0.19044
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

I have been using my VS Code PS terminal exclusively with no cmdlet issues until I tried using the SqlServer module.

Dave
  • 687
  • 7
  • 15
  • 1
    For a given PowerShell version, a discrepancy would only ariese if the list of directories listed `$env:PSModulePath` differs between host environments. Note that the PIC (PowerShell Integrated Console) that comes with Visual Studio Code's PowerShell extension has a different `$PROFILE` file than other host environments (regular console windows, Windows Terminal), so different `$PROFILE` files may (not) modify `$env:PSModulePath` differently. – mklement0 Feb 20 '23 at 03:24
  • @mklement0 The list of directories is the same for each terminal. As are the relevant `$PROFILE` files: `Test-Path $profile False` – Dave Feb 20 '23 at 03:38
  • 1
    What do `(Get-Module -ListAvailable SqlServer).Path` and `Import-Module SqlServer -Force` report? – mklement0 Feb 20 '23 at 13:08
  • 1
    Also, there are _multiple_ profiles, _two_ of which are host-specific. Submitting `$PROFILE` only shows one of them. To see them all and determine whether they exists, use `$PROFILE.psobject.properties.Where({ $_.Name -ne 'Length' }) | % { [pscustomobject] @{ Profile = $_.Name; Path = $_.Value; Exists = Test-Path $_.Value } }` – mklement0 Feb 20 '23 at 13:42
  • (The last comment is just a general aside, given that you state that `$env:PSModulePath` lists the same set of directories in both host environments.) – mklement0 Feb 20 '23 at 13:50
  • 1
    Thanks mklement0. Restarting VS Code sorted the problem out. Just as well. Because those combinations you provided were all the same between VS Code PS terminal & PS terminal. – Dave Feb 20 '23 at 18:01