Pages

Wednesday 28 August 2013

Powershell script to iterate through all Site Collections and Sub-sites for a Web Application

The Powershell script below iterates through all Site Collections and Sub-sites for a Web Application . It can be used for Running a Script on All Sites in a Web-Application




Add-PSSnapin Microsoft.Sharepoint.Powershell
[Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges({
$webApplicationURL = "http://sedwdevrtm/"
$webApp = Get-SPWebApplication $webApplicationURL

if($webApp -ne $null)
{
  Write-Host "Web Application : " + $webApp.Name

  foreach($siteColl in $webApp.Sites)
    {
     if($siteColl -ne $null)
      {
         Write-Host "Site Collection : " + $siteColl.Url
         foreach($subWeb in $siteColl.AllWebs)
          {
            if($subWeb -ne $null)
              {
                #Print each Subsite
                #Write-Host $subWeb.Url
                 Write-Host $subWeb
                 $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
});

1 comment: