in

Dot Net Mafia

Group site for developer blogs dealing with (usually) .NET, SharePoint 2013, SharePoint 2010, Office 365, SharePoint Online, and other Microsoft products, as well as some discussion of general programming related concepts.

This Blog

Syndication

Archives

Corey Roth [MVP]

A SharePoint MVP bringing you the latest time saving tips for SharePoint 2013, Office 365 / SharePoint Online and Visual Studio 2013.

Activating and Deactivating Features with PowerShell in SharePoint 2010

I was looking at the analytics of the site the other day and I noticed that my post, Adding and Deploying Solutions with PowerShell, currently has the most traffic on the site.  I had always intended to write a follow up post on this, so here it is.  Today I’m going to talk about how to enable and disable features using PowerShell.  It’s great that you know how to deploy solutions now with PowerShell, but now you want to activate your features.

In SharePoint 2007 to activate a feature from the command line you might have used a statement like the one below.  I’ll use the Reporting feature on my server named sp2010 as an example.  The value specified in the name parameter refers to the actual folder name of the feature that is in your SharePoint Root folder (14 hive).

stsadm.exe –o activatefeature –name Reportingurl http://sp2010

To get started with PowerShell, run the SharePoint 2010 Management Console located in your Microsoft SharePoint 2010 Products folder on your start menu.  This automatically loads the Microsoft.SharePoint.PowerShell snapin so that we can execute SharePoint commands.    You might have noticed earlier that I said we will talk about enabling and disabling features.  The words enable and disable are key here because PowerShell uses those verbs to activate and deactivate features.  To issue the same command above but with PowerShell, we use the Enable-SPFeature command.  The rest of the syntax is pretty similar as you can see below.  Just use –Identity to specify the name of the feature.  You can even pass the –Force command like you could with stsadm.exe.

Enable-SPFeature –Identity Reporting –url http://sp2010

If your command worked successfully, you will see nothing and just get a blank prompt back.

PowerShellEnableSPFeatureNoOutput

If you would like to get some feedback from the command, you can have it return the SPFeature object back to you by specifying the –PassThru parameter.

PowerShellEnableSPFeaturePassThru

We can confirm that the feature did in fact activate successfully using the SharePoint UI.

PowerShellReportingFeatureEnabledUI

Can we confirm that the feature is enabled with PowerShell?  Yes, we can by using Get-SPFeature.  Now this cmdlet behaves differently depending upon the parameters passed through it.  For example, if you type the following, it lists every feature on the SharePoint farm along with its Id and Scope.

Get-SPFeature

However, if you want to know which features are enabled, you can pass it a URL for a given scope (i.e.: –Web, –Site, –WebApplication, and –Farm).  So to get a list of all Site Collection scoped features, we would use the –Site parameter.

Get-SPFeature –Site http://sp2010

PowerShellGetSPFeatureSite

You can still specify a specific feature or even use Where-Object to query the list as well.

Get-SPFeature –Identity Reporting –Site http://sp2010

PowerShellGetSPFeatureSiteSpecific

If you get something like you see in the screenshot above, you know the feature has been activated.  When the feature hasn’t been activated, you will get a lovely red error message.

PowerShellGetSPFeatureSiteSpecificNotEnabled

The Enable-SPFeature command can also take a Pipe Bind from Get-SPFeature.  This means that you could even activate multiple features at once this way.  I’ll include an example of this in the future.

At some point, you will want to disable that feature you activated.  As you probably guessed, the cmdlet you want is Disable-SPFeature.  You use the same parameters that you use with Enable-SPFeature.  When you execute the command, it prompts for confirmation as you can see in the screenshot.

Disable-SPFeature –Identity Reporting –url http://sp2010

PowerShellDisableSPFeatureWithConfirmation

If you want to disable confirmation, you can pass the –confirm:$false parameter.

Disable-SPFeature –Identity Reporting –url http://sp2010 –Confirm:$false

PowerShellDisableSPFeatureNoConfirmation

Setting the confirm parameter eliminates the confirmation prompt and you can use it with other PowerShell cmdlets as well.  That is all that is involved in activating and deactivating features with PowerShell.  It’s pretty easy.   Hopefully, you can incorporate these commands in your deployment scripts.

Comments

 

Thomas Møller Jørgensen said:

Just a quick note: Remember quotes if the feature folder contains spaces. Alternatively, you can specify the feature ID.

May 5, 2011 4:41 AM
 

Venkatesh R said:

Thanks for explanation with screenshots.

May 5, 2011 11:15 PM
 

My resource to learn on 70-573 « martinbodocky said:

Pingback from  My resource to learn on 70-573   « martinbodocky

May 11, 2011 5:23 AM
 

Neville A. Williams said:

I am trying to move a site from a 2007 SharePoint in Domain A to a 2010 SharePoint in Domain B using STSADM. Microsoft says it can be done. But I am noticing a different behavior in 2010. How can I make this work?

October 4, 2011 1:13 PM
 

CoreyRoth said:

@Neville I'm afraid I haven't tried that particular scenario.

October 17, 2011 9:35 PM
 

Rick said:

Thanks for the info...clear and concise.

November 23, 2011 2:04 PM
 

Rick said:

Thanks for the info...clear and concise.

November 23, 2011 2:05 PM
 

Ish said:

This DOES NOT work if the feature has a feature receiver for activation.  The code does NOT run.

December 14, 2011 7:32 AM
 

Kourosh said:

Hi,

I have a feature which as a event reciever , I have added it to my WebTemplate's Onet.xml when you create a subweb by PowerShell the event reciever will trigged, now I want to debug it when i execute the powershell script, how can i to do it? thanks.

December 28, 2011 2:50 AM
 

CoreyRoth said:

@Kourosh Try this technique that I use with cmdlets.  It should work.  www.dotnetmafia.com/.../how-to-debug-a-powershell-cmdlet.aspx

January 5, 2012 3:48 PM
 

CoreyRoth said:

@Ish it should.  Make sure that your event receiver is not in the same assembly (or solution package as the feature you are activating).  It also need to be installed to the GAC if you are activating with PowerShell (although it probably already is).  Lastly, make sure the account you are using is in the Get-SPShellAdmin group.

January 5, 2012 4:04 PM
 

Amir Khan said:

Hi Cory

Thanks for the write up.

We have noticed a very strange behaviour when activating the PublishingSite site collection feature via the gui and via powershell and I'm wondering if you have any ideas or thoughts on it.

What we notice in all of our sharepoint farms that when we manually or via script activate the Publishing Site feature and waited it takes about 2-3 minutes before it shows that its activated.

Thiis is too long for us so we've been trying to understand where the bottleneck is. Lots of perf testing but nothing clear as of yet.

Then, I noticed something very strange and confusion!

If I activate the Publishing Site feature via the gui, wait 3 seconds, refresh the page and then click the activate button again the feature is activated in less then 10 seconds!

I proceed to activate the publishing web feature as well which is done instantly and everything is in place.

Can you explain this? Why does it go blazing fast after a refresh?

I would be very grateful if you had any thoughts to share.

Thank you

Amir

January 18, 2012 4:18 AM
 

sravan said:

This is a good post. we can easily understand how to enable and disable features using powershell command

January 19, 2012 3:33 AM
 

Nilesh said:

Cannot find an SPSite object with Id or Url: customer/.../default.aspx.

January 25, 2012 4:31 AM
 

tstojecki said:

For sandboxed solutions you will need to add the Sandboxed switch, e.g.: get-spfeature -Sandboxed -Site $siteUrl.

Get-SPFeature doesn't list out the sandboxed features without it.

February 6, 2012 10:36 AM
 

Trials and Tribulations: Migrating My Demos Site to Office365 – Part Two » Marc D Anderson's Blog said:

Pingback from  Trials and Tribulations: Migrating My Demos Site to Office365 – Part Two » Marc D Anderson's Blog

February 6, 2012 11:38 AM
 

CoreyRoth said:

@Nilesh make sure your account has proper permissions.  It should also be in the Shell Admin group visible by running Get-SPShellAdmin.

February 6, 2012 11:54 PM
 

CoreyRoth said:

@Amir  I haven't noticed it, but I'll give it a try when I have a chance and see if I can reproduce it.

February 7, 2012 12:11 AM
 

TerryB said:

@Corey, You made a fairly significant statement in "Make sure that your event receiver is not in the same assembly (or solution package as the feature you are activating)."

We are having endless pain deploying a solution and activating features via powershell. The last thing we have not tried is putting the feature receivers into a separate solution. I was hoping you could expand on the idea, or point to any documentation that supports the practise.

Thanks Terry

March 26, 2012 11:47 PM
 

CoreyRoth said:

@TerryB I've adopted this practice back in the 2007 days.  Since I used to run into issues activating features with stsadm in the same manner.  I'm not sure that there is any guidelines on this, but this is what I have been doing for some years.  

April 2, 2012 11:00 PM
 

Pradip said:

This is good explanation.

To find the site collections where a specific feature is activated, Check this. paddyt.blogspot.in/.../powershell-to-find-site-collections.html

April 11, 2012 11:07 PM
 

FranckF said:

This is a merveillous post where all elements are clearly explained.

Regarding the message from @Amir, we are facing the same issue. We are developping a lot of publishing sites and today, we have to create "delay sections" or reactive twice the feature to really active it.

If you have any idea / solution, it would be great.

Thank you a lot.

June 7, 2012 3:54 PM
 

SP2010Tq said:

Thanks for the snapshots.  Really Helps.  One additional note on the commands if you have site(s) defined with an PORT number use it as:  

get-spfeature -site http://sp2010dev:22511

August 7, 2012 10:37 AM
 

???????? ???????? Feature ???? ???? ?????????????? | ?????????? said:

Pingback from  ???????? ???????? Feature ???? ???? ?????????????? | ??????????

October 28, 2012 2:13 AM
 

Moving sites templates between servers and with feature dependencies said:

Pingback from  Moving sites templates between servers and with feature dependencies

November 16, 2012 10:01 AM
 

BGM said:

This is excellent!  Thank you very much!

November 22, 2012 11:58 PM
 

???????? ???????? Feature ???? ???? ?????????????? | ?????????? said:

Pingback from  ???????? ???????? Feature ???? ???? ?????????????? | ??????????

June 12, 2013 2:16 PM
 

Revanth said:

hi

features with event receivers are activating but event receivers are not firing when executed through power shell script

June 27, 2013 8:02 AM

Leave a Comment

(required)  
(optional)
(required)  
Add

About CoreyRoth

Corey Roth is an Applications Architect at Infusion specializing in ECM and Search.
2012 dotnetmafia.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems