SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am looking for a way to run PowerShell from C# code. What is more, I need the code to activate the following features in my Sharepoint site that gets created:

1) SharePoint Server Publishing Infrastructure


2) Sharepoint Server Publishing

How would I do this for PowerShell code in C#?

share|improve this question
I'm curious, why PowerShell through C# and not just activate from C# code? Also, which version of SharePoint are you using? – Louis Apr 4 '12 at 21:54
Yeah, it does seem unnecessary. Unless you're trying to run a cmdlet who's function isn't exposed in the SharePoint object model. – RJ Cuthbertson Apr 5 '12 at 0:59

Check this out to activate features in C#:

And a list of SharePoint OOTB feature guids:


If you really want to run PowerShell from C#:

Don't forget to add the SharePoint snapin to expose the cmdlets:

Add-PSSnapin Microsoft.SharePoint.PowerShell
share|improve this answer

PowerShell & C# are two languages you can use to access the SharePoint Object Model. Depending upon the kind of task you want to perform on SharePoint platform, you can choose any one. In general, almost every task is achievable using C# or Powershell

Using C#, If you want to activate a feature when a new site is created, below is the code you can use in WebProvisioned Event Receiver.

Guid featureGuid = new Guid(YourFeatureGUID);
if (web.Features[featureGuid] == null) 

By the way, I would recommend to create the site using Publishing Site definition so that you get all publishing features activated by default.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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