title: How to fix error "Cannot load PowerShell snap-in Microsoft.PowerShell.Diagnostics because of the following error: Could not load file or assembly"
body: <p>I'm trying to execute a Powershell script from an asp.net webpage and I keep getting this error when it tries to execute the script. I have tried 2 methods of executing the script and both give me the same error. The full error is: </p>
  
  <blockquote>
    <p>An unhandled exception occurred while processing the request
    PSSnapInException: Cannot load PowerShell snap-in
    Microsoft.PowerShell.Diagnostics because of the following error: Could
    not load file or assembly
    'C:\source\repos\WebApp\WebApp\Microsoft.PowerShell.Commands'. The
    system cannot find the file specified.</p>
  </blockquote>
  
  <pre><code>PowerShell powershell = PowerShell.Create();
              using (Runspace runspace = RunspaceFactory.CreateRunspace())
              {
                  runspace.Open();
                  powershell.Runspace = runspace;
                  System.IO.StreamReader sr = new System.IO.StreamReader("C:\\Desktop\\test.ps1");
                  powershell.AddScript(sr.ReadToEnd());
                  var results = powershell.Invoke();
                  if (powershell.Streams.Error.Count &gt; 0)
                  {
                      // error records were written to the error stream.
                      // do something with the items found.
                  }
              }
  </code></pre>
  
  <pre><code>    using (PowerShell PowerShellInstance = PowerShell.Create())
                  {   
    PowerShellInstance.AddScript("C:\\Users\\RSpotton\\Desktop\\test.ps1");
                      PowerShellInstance.Invoke();
                  }
  </code></pre>