Powershell to Loop through all SharePoint sites and pages

## SharePoint 2007 DLL
## If you use SharePoint 2010 PowerShell, this is not required
[System.Reflection.Assembly]::Load(“Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)
[System.Reflection.Assembly]::Load(“Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)
 
$siteURL = "http://localhost/"
set-variable -option constant -name out -value "C:\temp\PrintAllSitesSubsites.csv"
$spSite = [Microsoft.SharePoint.SPSite] ($siteURL)
 
if($spSite -ne $null)
{
   "Site Collection : " + $spSite.Url | Out-File $out -Append
   foreach($subWeb in $spSite.AllWebs)
   {
      if($subWeb -ne $null)
      {
         #Print each Subsite
         #Write-Host $subWeb.Url
         "Subsite : " + $subWeb.Name + " - " + $subWeb.Url | Out-File $out -append
 
         $spListColl = $subweb.Lists
         foreach($eachList in $spListColl)
         {
            if($eachList.Title -eq "Pages")
            {
               $PagesUrl = $subweb.Url + "/"
               foreach($eachPage in $eachList.Items)
               {
                  "Pages : " + $eachPage["Title"] + " - " + $PagesUrl + $eachPage.Url | Out-File $out -append
               }
            }
         }
         $subWeb.Dispose()
      }
      else
      {
         Echo $subWeb "does not exist"
      }
   }
   $spSite.Dispose()
}
else
{
   Echo $siteURL "does not exist, check the site collection url"
}
Echo Finish

Leave a Reply

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