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