I am trying make a c# application which runs sharepoint cmdlets in management shell 2010.

I have tried below code:

protected void ExecuteCode_Click(object sender, EventArgs e)

            RunspaceConfiguration config = RunspaceConfiguration.Create();

            PSSnapInException OExSnapIn = null;

            PSSnapInInfo pssnap = config.AddPSSnapIn("Microsoft.SharePoint.PowerShell", out OExSnapIn);

            Runspace cmdlet = RunspaceFactory.CreateRunspace(config);


            RunspaceInvoke scriptInvoker = new RunspaceInvoke(cmdlet);

            scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted");

             Pipeline pipeline = cmdlet.CreatePipeline();

            Command cmd = new Command("Get-SPFarm");



            Command cmdnew = new Command("Get-SPWebApplication");



            IEnumerable<PSObject> output = pipeline.Invoke();



            StringBuilder results = new StringBuilder();

            foreach (PSObject obj in output)





but it throws an exception like this

enter image description here

note: I found out that sharepoint management 2010 will load powershell 3.0 and as my code targets 2.0 it is throwing weird errors.

even in the sharepoint management shell window I get this below error:

enter image description here

I have installed all the updates also.. I found that I need to uninstall an update like this " Windows6.1-KB2506143-x64" I did it still am clueless of what am missing.

hope am clear to u. Any help is much appreciated.

in simple terms I want to see the output in the output below : enter image description here

  • to add to this: am using 64bit machine.. trying to use sharepoint 2010 – Keerthi Feb 12 '14 at 7:45

In c# use .net framework 3.5 - then it might work. For PowerShell console: Edit the powershell shortcut and add "-version 2" behind the powershell.exe (in your startmenu or toolbar).

  • hi @max: thanks a ton for response.. I have already set the target for the powershell console as "C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -Version 2.0 -NoExit " & ' C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration\\sharepoint.ps1 ' " but it didn't help – Keerthi Feb 12 '14 at 10:44
  • and regarding the framework: if I change it to 3.5 it says.. Error..The type or namespace name 'Automation' does not exist in the namespace 'System.Management' (are you missing an assembly reference?) – Keerthi Feb 12 '14 at 10:47

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.