title: Running powershell scripts from within a .NET windows app
body: <p>I'm needing to run scripts from within a vb.net windows app.</p>
  
  <p>I've got the scripts running in the background fine;</p>
  
  <pre><code> Using MyRunSpace As Runspace = RunspaceFactory.CreateRunspace()
        MyRunSpace.Open()
        Using MyPipeline As Pipeline = MyRunSpace.CreatePipeline()
          MyPipeline.Commands.AddScript("import-module -name " &amp; moduleName &amp;
                                vbCrLf &amp;
                                "(get-module -name " &amp; moduleName &amp; ").version")
  
  
          Dim results = MyPipeline.Invoke()
          'Do something with the results
        End Using
  
        MyRunSpace.Close()
      End Using
  </code></pre>
  
  <p>However, i now need to be able to have the powershell run (not in the background)  eg.  When prompts occur;</p>
  
  <blockquote>
  <pre><code>Set-ExecutionPolicy unrestricted
  </code></pre>
  </blockquote>
  
  <p>I'm currently looking into the Microsoft.PowerShell.ConsoleHost namespace to see if i can use something like;</p>
  
  <pre><code>Dim config = RunspaceConfiguration.Create
  ConsoleShell.Start(config, "Windows PowerShell", "", New String() {""})
  </code></pre>
  
  <p>Can anyone advise me please??? </p>
  
  <p>EDIT: I've fudged it a bit with this;</p>
  
  <pre><code>  Public Function RunPowershellViaShell(ByVal scriptText As String) As Integer
      Dim execProcess As New System.Diagnostics.Process
      Dim psScriptTextArg = "-NoExit -Command ""&amp; get-module -list"""
      'Dim psScriptTextArg = "-NoExit -Command ""&amp; set-executionPolicy unrestricted"""
      'Dim psScriptTextArg = ""-NoExit -Command """ &amp; scriptText &amp; """"
  
      execProcess.StartInfo.WorkingDirectory = Environment.SystemDirectory &amp; "\WindowsPowershell\v1.0\"
      execProcess.StartInfo.FileName = "powershell.exe"
      execProcess.StartInfo.Arguments = psScriptTextArg
      execProcess.StartInfo.UseShellExecute = True
      Return execProcess.Start
    End Function
  </code></pre>
  
  <p>But there's gotta be a better way??</p>