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.