title: Powershell script to list all sharepoint 2010/2007 pages and their layouts
body: <p>I'm trying to write a powershell script which will run all over my sharepoint 2010/2007 pages and will list their page layout and will save the results into a text file.</p>
  
  <p>while running the following script which i worte, i get after acouple of minuets an error:</p>
  
  <p>EnumeratePages : Exception has been thrown by the target of an invocation.
  At T:\listpages.ps1:75 char:15
  + EnumeratePages &lt;&lt;&lt;&lt; ('http://preportal.idc.ac.il')
      + CategoryInfo          : NotSpecified: (:) [EnumeratePages], TargetInvocationException
      + FullyQualifiedErrorId : System.Reflection.TargetInvocationException,EnumeratePages</p>
  
  <pre><code># Add SharePoint cmdlets reference
  Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue 
  
  function EnumeratePages($Url) {
      $site = new-object Microsoft.SharePoint.SPSite $Url 
  
      foreach($web in $site.AllWebs) {
          if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web)) {
              $pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
              $pages = $pWeb.PagesList
  
              Write-Host "Processing Web:" $pWeb.Url "..." -ForegroundColor Magenta
  
              foreach ($item in $pages.Items) {
                  $fileUrl = $pWeb.Url + $webUrl + "/" + $item.File.Url
                  Write-Host "   " $fileUrl -ForegroundColor Green
                  foreach ($fld in $item.Fields)
                      {
                      if($fld.Title -and $fld.InternalName -and $item[$fld.InternalName])
                      {
                       if($fld.InternalName -eq "PublishingPagelayout")
                       {
                        Write-Host "PublishingPagelayout: " + $item[$fld.InternalName].ToString()
                        Select "Page Url: ", $fileUrl, "PublishingPagelayout: ", $item[$fld.InternalName].ToString() | Format-List
                       }
                      }
  
                      #$spFile = $web.GetFile($fileUrl.ToString())                               
                      #if($spFile.Properties.Count -gt 0)
                      #{
  
                      #}
                  }                
              }
          }
          else {
              Write-Host "   Not a publishing web:" $web.Url". Looking for Site Pages library." -ForegroundColor Magenta
              $pages = $null
              $pages = $web.Lists["Site Pages"]
  
              if ($pages) {
                  Write-Host "   " $pages.Title "found." -ForegroundColor Green
                  foreach ($item in $pages.Items) {
                      $fileUrl = $pWeb.Url + $webUrl + "/" + $item.File.Url                               
                      Write-Host "   " $fileUrl -ForegroundColor Green    
                      foreach ($fld in $item.Fields)
                      {
                          if($fld.Title -and $fld.InternalName -and $item[$fld.InternalName])
                          {
                           if($fld.InternalName -eq "PublishingPagelayout")
                           {
                            Write-Host "PublishingPagelayout: " + $item[$fld.InternalName].ToString()
                            Select "Page Url: ", $fileUrl, "PublishingPagelayout: ", $item[$fld.InternalName].ToString() | Format-List
                           }
                          }
                      }
                      #$spFile = $web.GetFile($fileUrl)                   
                      #if($spFile.Properties.Count -gt 0)
                      #{
  
                      #}
              }
              else {
                  Write-Host "    Site Pages library not found." -ForegroundColor Red
              }
          }
  
          Write-Host "... completed processing" $web "..." -ForegroundColor Magenta
      }
  }
  }
  
  
  $row = EnumeratePages('http://server-name')
  $row &gt; t:\SitePagesPropertiesReport2.txt
  </code></pre>
  
  <p>Please advise.</p>