by

How To Count How Many Pages in SharePoint Site Using PowerShell

Some tasks you don't need to go ask the developers to create a feature for you. Don’t worry about them.
Again, here’s another script for the casual SharePoint Administrator, Developer or Project Manager that does not require much work or knowledge of SharePoint. How many pages a site has? Let’s go to the bits. You can use this little script below:

function count-items-by-site-by-list-type($URL, $BASETEMPLATE) 
{     
    $site = Get-SPSite $URL     
    $totalItems = 0     
    foreach($web in $site.AllWebs)     
    {         
        $lists = $web.Lists         
        for ($i=0; $i -le $lists.Count – 1; $i++)          
        {             
            $list = $lists[$i]             
            if ($list.BaseTemplate -eq $BASETEMPLATE)             
            {                 
                $totalItems = $totalItems + $list.Items.Count             
            }         
        }             
    }     
    $site.Dispose()     
    write-host $totalItems 
}




This script counts all the items for a given list type. So for example, if you want to count how many pages there are in the following http://www.myProjectSite.com you should do this:


count-items-by-site-by-list-type http://mySite/ "WebPageLibrary"


You can get any result; project items, total of tasks, total of persons, total of documents…anything. The possible values for the $BASETEMPLATE parameter can be found in this link.


What if I have pages not only in page libraries but across any library?


Then you can use this modified version. Remember this code will run a bit slower so I’ve put some visual clues as heartbeat indicators.



function count-pages($URL)
{    
    $site = Get-SPSite $URL    
    $totalPages = 0    
    foreach($web in $site.AllWebs)     
    {         
        write-host        
        write-host "Processing:" $web.Url        
        $lists = $web.Lists        
        write-host "(lists found ):" $lists.Count        
        write-host "-------------------"        
        for ($i=0; $i -le $lists.Count – 1; $i++)        
        {             
            try            
            {                
                $list = $lists[$i]                
                for ($j=0; $j -le $list.Items.Count – 1; $j++)                
                {                    
                    $item = $list.items[$j]                    
                    if ( $item.Url.ToLower().EndsWith(".aspx") )                    
                    {                        
                        $totalPages++                        
                        write-host -fore green -back green " " -nonewline                    
                    }                
                }            
            }            
            catch                
            {                
                write-host -fore red -back red " " -nonewline            
            }        
        }    
    }    
    $site.Dispose()    
    write-host    
    write-host "----------------------------------"    
    write-host "total pages found--->" $totalPages
}



Cheers,

By

4 comments:

  1. Very Nice. I need to test this. By chance, would you know how to count how many aspx pages on site that contain a particular string?
    How about number of infopath forms? or pages with a particular webpart?

    ReplyDelete
  2. Also, please append to my prevoius question. Do you know how to get the total number of objects (not just pages) on any given site collection or site?

    ReplyDelete
  3. sure, mostly everything can be counted in sharepoint, however the count method varies from one object to other. what kinds of objects are you interested in counting?

    ReplyDelete
  4. sure. to count the number of pages is just a simple loop through the page libraries in the web. To count the pages with a specific string in the name just apply a filter using the method "contains" in the name, of url property of the item.

    To count the infopath forms, it is the same methodology, but instead of looping through the page libraries you will be looking at form libraries.

    ReplyDelete