title: Powershell SQLPS module not importing within C#
body: <p>I'm attempting to execute a SQL Query from within Powershell, within C#. I have been successful in doing so with ActiveDirectory cmdlets and wanted to take it one step further.</p>
  <p>My first issue is while the following format works with ActiveDirectory (and in the ISE) it fails in C#:</p>
  <pre><code>using (PowerShell pS = PowerShell.Create())
      {
          pS.AddCommand(&quot;import-module&quot;);
          pS.AddArgument(&quot;sqlps&quot;);
          pS.Invoke();
      }
  </code></pre>
  <p>I've long since had the security set to Unrestricted, but the error I'm getting is:</p>
  <blockquote>
  <p><strong>CmdletInvocationException was unhandled</strong></p>
  <p>File C:\Program Files (x86)\Microsoft SQL Server\110\Tools\PowerShell\Modules\sqlps\Sqlps.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at <a href="http://go.microsoft.com/fwlink/?LinkID=135170" rel="nofollow noreferrer">http://go.microsoft.com/fwlink/?LinkID=135170</a>.</p>
  </blockquote>
  <p>However, if I run like this I get no error, though a later &quot;Get-Module -all&quot; call shows no sign of the module:</p>
  <pre><code>using (PowerShell pS = PowerShell.Create())
      {
          pS.AddScript(&quot;Import-Module sqlps&quot;);
          pS.Invoke();
      }
  </code></pre>
  <p>If I then try importing the ActiveDirectory module and calling Get-Module, it shows nothing.</p>
  <p>What's going on here?</p>