Powershell SqlPs module not importing properly

400 Views Asked by At

I have a (pretty much clean) Windows Server 2008 R2 Build with SQL Server 2012 installed. I'm having a problem with PowerShell (Version 3).

I am trying to use the Invoke-Sqlcmd cmdlet. However, when I call get it i get a message saying that 'Invoke-Sqlcmd' is not recognized as the name of a cmdlet, function, script file, or operable program.

It works if if I run Import-Module SqlPs -Verbose -Force (bypassing the security policy). However it this lasts only as long as the tab is open. If i open a new tab, try to run another script or re-open Powershell ISE I have to import the module all over again.

Any ideas why this is happening?

2

There are 2 best solutions below

0
On

From my understanding, module loading is done on a per session basis. So the behavior you're seeing is what is expect.

0
On

This is more of a work around, but you can add it to your profile.

"'Import-Module SqlPs -Verbose -Force'" > $profile

Obviously if you have a profile file from before, you dont want to do it like that, but instead open it and add it through notepad or similar.

Also note that Powershell and PowershellISE has different profiles, so you would need to add it in both if you mix.