I use a PS script to create a list. Instead of a scheduled task, I wondered if I can put a link or a button on a sharepoint page, that when you click on it, it runs a PS Script on the SharePoint server (with Administrator privilege).

I saw this plugin that allows us to run PS script within a workflow. This is a cool thing. But I prefer running the script whenever I want (or my users want).

Do you have a good reference for this ? I saw this one. But not sure about what I should Googled.


Hey, I'm back.

I manage to create a empty projet thanks to this.

So now, from my workstation (not SP server), I'm able to use PS script or commands. I also manage to use a dropdown list and select which script I want to execute.

I can't find how to add this custom application on my SP site (I have a aspx page, and aspx.cs). Well a kind of WebApplication if I'm right.

  • you want to hard code the script in C# or you want to load the script dynamically? Jul 7, 2014 at 12:09

2 Answers 2


You could create a Custom WebPart. A simple button Handler could execute the under given code to fire your PowerShell script.

scriptText parameter is the script which you should pass. You can HardCode it or load it from any file.

private string RunScript(string scriptText)
    // create Powershell runspace

    Runspace runspace = RunspaceFactory.CreateRunspace();

    // open it


    // create a pipeline and feed it the script text

    Pipeline pipeline = runspace.CreatePipeline();

    // add an extra command to transform the script
    // output objects into nicely formatted strings

    // remove this line to get the actual objects
    // that the script returns. For example, the script

    // "Get-Process" returns a collection
    // of System.Diagnostics.Process instances.


    // execute the script

    Collection<psobject /> results = pipeline.Invoke();

    // close the runspace


    // convert the script result into a single string

    StringBuilder stringBuilder = new StringBuilder();
    foreach (PSObject obj in results)

    return stringBuilder.ToString();


Please let me know if you want further clarification.

  • Please mark it as answer if it solves your query Jul 7, 2014 at 12:18
  • This is an answer. But now, I'm not enable to add this custom application in my SP test site. Updated question
    – Nico
    Jul 16, 2014 at 10:39

Very important addition. This command dont launch without next edits:

  1. This code need use in SPSecurity.RunWithElevatedPrivileges(delegate() { // code above });
  2. The account under which the portal is launched must have rights, in particular, be in the group "Recipient manager"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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