SharePoint Dragons

Nikander & Margriet on SharePoint

Appreciating a simple idea about running PowerShell Scripts from SharePoint

It’s a simple idea to want to be able to execute PowerShell scripts from code, as you can see in The code is something similar to this:

PowerShell ps = PowerShell.Create();

string scriptText = “Add-PSSnapin Microsoft.SharePoint.Powershell Enable-SPFeature -Identity PublishingSite -URL ” + siteAddress + ” -Force”;

Runspace runspace = RunspaceFactory.CreateRunspace();


Pipeline pipeline = runspace.CreatePipeline();



Collection<PSObject> results = pipeline.Invoke();


StringBuilder stringBuilder = new StringBuilder();

foreach (PSObject obj in results)




return stringBuilder.ToString();


In the blog post this idea is taken to the next level by allowing you to run PowerShell scripts in the SharePoint Timer Service context. Really useful, and elegantly done!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: