title: Accessing sharepoint from Powershell-script from c# application
body: <p>Hello I'm trying to access a sharepoint list from powershell. The powershell script is executed from a c# application.</p>
  
  <p>If I run the from c# application I get the following exception:</p>
  
  <blockquote>
    <p>Exception calling ".ctor" with "1" argument(s): "The Web application
    at <code>http://pc/websites/Test4/</code> could not be found. Verify that you have
    typed the URL correctly. If the URL should be serving existing
    content, the system administrator may need to add a new request URL
    mapping to the intended application."</p>
  </blockquote>
  
  <p><strong>My Code:</strong></p>
  
  <p><strong>C#</strong></p>
  
  <pre><code>     string cmdArg = "C:\\Scripts\\GroupChangeGroup.ps1 1";
                      Runspace runspace = RunspaceFactory.CreateRunspace();
                      runspace.ApartmentState = System.Threading.ApartmentState.STA;
                      runspace.ThreadOptions = PSThreadOptions.UseCurrentThread;
                          runspace.Open();
                      Pipeline pipeline = runspace.CreatePipeline();
  
   Command newcom = new Command("Add-PSSnapin");
                        newcom.Parameters.Add("Name", "microsoft.exchange.management.powershell.e2010");
                        pipeline.Commands.Add(newcom);
  
                    Command newcom2 = new Command("Add-PSSnapin");
                    newcom2.Parameters.Add("Name", "Microsoft.SharePOint.POwershell");
                    pipeline.Commands.Add(newcom2);
  
                      pipeline.Commands.AddScript(cmdArg);
                      pipeline.Commands[0].MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);
                      Collection&lt;PSObject&gt; results = pipeline.Invoke();
                      var error = pipeline.Error.ReadToEnd();
                      runspace.Close();
  </code></pre>
  
  <p><strong>Powershell</strong></p>
  
  <pre><code>$site = New-Object Microsoft.SharePoint.SPSite("http://pc/websites/Test4/")
  $web = $site.OpenWeb()
  $list = $web.Lists["GroupsList - ListInstance1"]
  $listitem = $list.Items.GetItemByID(3)
  </code></pre>
  
  <p>If I execute the application from PS there is no problem. (same user context)</p>
  
  <p>The User is Member of <code>WSS_ADMIN_WPG</code></p>