5

I am trying to run PowerShell command from a c# application against SharePoint. To do this i need to include the SharePoint powershell snap-in. The code to do this is below.

    using (PowerShell psi = PowerShell.Create())
    {
        psi.Commands.AddCommand("Add-PSSnapin").AddParameter("Name", "Microsoft.SharePoint.PowerShell");
        Collection<PSObject> PSOutput = psi.Invoke();
    }

When executed the error is:

The Windows PowerShell snap-in 'Microsoft.SharePoint.PowerShell' is not installed on this computer.

If i run the same command from PowerShell console it works fine. Can someone please explain why it wont load this from C# but will work from PowerShell console.

3
  • 1
    Do you have the correct framework version targeted for your application, as well as making sure it is x64? – Trevor Seward Apr 7 '17 at 2:17
  • I did not and i cannot believe i didn't think of that. Much appreciated on suggestion. Working now. – Simon Darlow Apr 7 '17 at 3:11
  • Added that comment as an answer. Glad it worked for you. – Trevor Seward Apr 7 '17 at 14:43
6

When using SharePoint PowerShell via C#, your VS project must be at .NET 4.0 or higher. In addition, the application must be built as an x64 application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.