Friday, February 10, 2012

Powershell script to check whether SharePoint 2010 Feature is activated for a particular Sub-site

The Powershell script below checks whether any given feature, in this case its the PublishingWeb Feature is activated or not for a particular Sub-Site.


Here I have shown 2 approaches: 1st one checks the Feature Folder Name and 2nd approach checks the FeatureID activated for that particular Sub-site. Choose the way best suitable in your case.


Add-PsSnapin Microsoft.SharePoint.PowerShell


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


$featureFolderName = "PublishingWeb"
$subSiteURL = "http://sharepointfix/sites/home/USA"


#Approach 1 - Check using Feature Name
#Get Feature ID based on the Feature Name and ensure whether its already activated or not at the current sub-site scope
$FeatureID = Get-SPFeature -Web $subSiteURL | Where {$_.DisplayName -eq $featureFolderName}


if($FeatureID -ne $null)
     {
      #Approach 2 -  Check using Feature ID
      #Check whether Feature to be activated is already activated for this sub-site
      if (Get-SPFeature -Web  $subSiteURL | Where {$_.ID -eq $FeatureID.Id})
      {
       Write-Host $featureFolderName "is already activated at :" $subSiteURL 
       }
      else
      {
       Enable-SPFeature -Identity $featureFolderName -Confirm:$false -Url $subSiteURL 
       Write-Host $featureFolderName "has been activated at :" $subSiteURL
      }
     }
else
{
      Enable-SPFeature -Identity $featureFolderName -Confirm:$false -Url $subSiteURL 
      Write-Host $featureFolderName "has been activated at :" $subSiteURL 
}


Remove-PsSnapin Microsoft.SharePoint.PowerShell


Echo Finish

Thursday, February 9, 2012

PowerShell script to Enumerate Sites, Sub-sites and print them in a .csv file

The Powershell script below enumerates/iterates through all Site Collections and Sub-sites for a Web Application and prints the output in a .csv file.

1. Copy the code below and modify the variables highlighted in yellow below, save the following as IterateAllSitesSubsites.ps1 file:


Add-PsSnapin Microsoft.SharePoint.PowerShell

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

$webApplicationURL = "http://dev-sp-2010:1000"
set-variable -option constant -name out -value "C:\PrintAllSitesSubsites.csv"

$webApp = Get-SPWebApplication $webApplicationURL

if($webApp -ne $null)
{
"Web Application : " + $webApp.Name | Out-File $out -Append

foreach($siteColl in $webApp.Sites)
{
   if($siteColl -ne $null)
   {
"Site Collection : " + $siteColl.Url | Out-File $out -Append

foreach($subWeb in $siteColl.AllWebs)
{
if($subWeb -ne $null)
{
#Print each Subsite
#Write-Host $subWeb.Url
"Subsite : " + $subWeb.Name + " - " + $subWeb.Url | Out-File $out -append
                 
$subWeb.Dispose()
}
else
{
Echo $subWeb "does not exist"
}
}
$siteColl.Dispose()
}
else
{
Echo $siteColl "does not exist"
}
}
}
else
{
Echo $webApplicationURL "does not exist, check the WebApplication name"
}

Remove-PsSnapin Microsoft.SharePoint.PowerShell

Echo Finish

2. To automatically run the above .ps1 script as a batch utility, Copy and paste the code below and save it with a .bat file extension

cd /d %~dp0
powershell -noexit -file ".\IterateAllSitesSubsites.ps1" "%CD%"
pause

Run the above .bat file, on the receipt of success message, traverse to the configured path and find the .csv file with all the sites/sub-sites printed.

Here is an alterante way of doing the same thing in a quick way using Powershell CMD:

Get-SPWebApplication "http://SPFix/" | Get-SPSite -Limit All | Get-SPWeb -Limit All | Select Title, URL, ID | Export-CSV C:\IterateAllSitesSubsites.ps1.csv -NoTypeInformation