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

## Reference to SharePoint DLL
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
## Probable location of sharepoint STSADM utility program
$stsadm = "$env:programfiles\Common Files\Microsoft Shared\Web Server Extensions\12\BIN\STSADM.EXE" 
 
############################################
# Activate-feature-onSite [-feature  |-url  ]
############################################
function global:Activate-feature-onSite($feature, $url)
{
$spsite=new-object Microsoft.SharePoint.SPSite($url); 
 
for($i=0; $i -lt $spsite.AllWebs.Count;$i++){
   $websiteurl=  $spsite.AllWebs[$i].url;
   $sResult = &stsadm -o activatefeature -name $feature -url $websiteurl -force
   if(!($sResult -like "*Operation completed successfully*")){
      Write-Host -ForegroundColor "red" -BackgroundColor "white" "Activate of feature '$feature' for '$websiteurl' Failed! `n $sResult"
     }
   }
$spsite.Dispose();
} 
 
# to call
Activate-feature-onSite "feature" "http://site"

[ad#ad-4]

Be Sociable, Share!
This entry was posted in .NET, PowerShell, SharePoint, WSS and tagged , , , , , , . Bookmark the permalink.
  • https://sharepoint.umich.edu Composer

    Thanks for posting this!

  • Brandon

    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?

  • Byung

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

  • Byung

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

  • david

    Thanks for the update

  • http://www.ekhichdi.com Tina

    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.

  • david

    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