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.
## 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" |
Thanks for posting this!
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?
Thanks for your sharing.
By the way, $sResult does not capture error message. Any idea?
it works with the following;
$outputmsg = &stsadm -o activatefeature -name abc -url http://mapdbjung2 2>&1
Thanks for the update