David's technobabble Rotating Header Image

Using PowerShell to activate a feature across all Sharepoint 2007/WSS 3.0 sites and subsites

Recently, I was deploying a new feature to a WSS 3.0 site and needed to activate the feature on roughly 100 subsites. I did not want to do this manually with the web interface. I knew that I could write a C# program to do this, but I’d read a lot about the capabilities of PowerShell and decided to see how this could be done with PowerShell.

After downloading and installing PowerShell 1.0 on my Windows 2003 test server, I spent some time getting familiar with PowerShell and reading what others had done. I found a very helpful site to be Zach Rosenfield’s blog and the Microsoft.SharePoint class reference on msdn. Based on my experience building custom workflows for sharepoint, I knew that if could obtain the list of website urls for a sharepoint site, then I could then call the STSADM program to activate the feature by name for the site by URL

    stsadm -o activatefeature -name feature-name -url websiteurl -force

The synopsis of the code is:

  • Start with the sharepoint site object.
  • Loop through all of the sub-sites.
    • Get the website url for the sub-site.
    • Call stsadm to activate the feature for the sub-site.


The PowerShell code

  1. ## Reference to SharePoint DLL
  2. [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
  3. ## Probable location of sharepoint STSADM utility program
  4. $stsadm = "$env:programfiles\Common Files\Microsoft Shared\Web Server Extensions\12\BIN\STSADM.EXE"
  5.  
  6. ############################################
  7. # Activate-feature-onSite [-feature  |-url  ]
  8. ############################################
  9. function global:Activate-feature-onSite($feature, $url)
  10. {
  11. $spsite=new-object Microsoft.SharePoint.SPSite($url);
  12.  
  13. for($i=0; $i -lt $spsite.AllWebs.Count;$i++){
  14.    $websiteurl=  $spsite.AllWebs[$i].url;
  15.    $sResult = &stsadm -o activatefeature -name $feature -url $websiteurl -force
  16.    if(!($sResult -like "*Operation completed successfully*")){
  17.       Write-Host -ForegroundColor "red" -BackgroundColor "white" "Activate of feature ‘$feature’ for ‘$websiteurl’ Failed! `n $sResult"
  18.      }
  19.    }
  20. $spsite.Dispose();
  21. }
  22.  
  23. # to call
  24. Activate-feature-onSite "feature" "http://site"

7 Comments

  1. Composer says:

    Thanks for posting this!

  2. Brandon says:

    This is great thanks for posting this. Do you know how I can get it so it will activate a feature on all sites across the web application?

  3. Byung says:

    Thanks for your sharing.
    By the way, $sResult does not capture error message. Any idea?

  4. Byung says:

    it works with the following;
    $outputmsg = &stsadm -o activatefeature -name abc -url http://mapdbjung2 2>&1

  5. david says:

    Thanks for the update

  6. Tina says:

    I have installed Powershell on wss 3.0 server bu I am unable to excute any command of powershell related to WSS 3.0. Is there something more that I need to do after installing PowerShell on WSS 3.0.

  7. david says:

    Hi Tina,
    Did you install powershell 1.0 or 2.0? and can you provide more info about any errors or messages that you recieve?

    I ran this with powershell 1.0 and all that I had to do was add the reference line

    ## Reference to SharePoint DLL
    [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

    Regards,
    David

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <font color="" face="" size=""> <span style="">

Bad Behavior has blocked 522 access attempts in the last 7 days.