,

SharePoint PowerShell Script Series Part 4–Gathering item count for all document libraries in a Site Collection

Posted by

I apologize it’s been so long since I’ve published any new blog content.  I recently moved into a new position in Microsoft as a Premier Field Engineer.  I’m really enjoying it so far and will start pumping out new blog content soon.  For now, I recently wrote a PowerShell script which fetches the item count for items that reside in all Document Libraries for a given Site Collection.  That includes all Document Libraries that exists in both the root site and all sub sites. 

The script will produce the following output:

1.    Total # of Document Libraries with 0 items
2.     Total # of Document Libraries with 1 or more items
2.    Total # of items in all Document Libraries
3.    Total # of items in DocLib\Subfolders
4.     Start Time and End Time of Script

 

Instructions for running the script:  
 
1.    Copy the below script and save it in notepad
2.    Save it with a anyfilename.ps1 extension
3.    To run, copy the file to a SharePoint Server
4.    Select Start\Microsoft SharePoint 2010 Products\SharePoint 2010 Management Shell
5.    Browse to directory holding the copied script file
6.    Run the script:  .\anyfilename.ps1 (assuming anyfilename is the name of the file)

Script is below:

<# ==============================================================
// Microsoft provides programming examples for illustration only,
// without warranty either expressed or implied, including, but not
// limited to, the implied warranties of merchantability and/or
// fitness for a particular purpose.
//
// This sample assumes that you are familiar with the programming
// language being demonstrated and the tools used to create and debug
// procedures. Microsoft support professionals can help explain the
// functionality of a particular procedure, but they will not modify
// these examples to provide added functionality or construct
// procedures to meet your specific needs.

// Author: Russ Maxwell (russmax@microsoft.com)
// ———————————————————-  #>

[Void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)
Start-SPAssignment -Global

$starttime = Get-Date
#Creating new site object
$siteurl = Read-Host “Enter the name of your site and press enter”
$site = New-Object Microsoft.SharePoint.SPSite($siteurl)

#Assigning all webs (sites) to $webs
$webs = $site.Allwebs

####################################################
#System Libraries defined so they won’t be touched##
####################################################

$systemlibs =@(“Converted Forms”, “Customized Reports”, “Documents”, “Form Templates”, 
                              “Images”, “List Template Gallery”, “Master Page Gallery”, “Pages”, 
                               “Reporting Templates”, “Site Assets”, “Site Collection Documents”,
                               “Site Collection Images”, “Site Pages”, “Solution Gallery”, 
                               “Style Library”, “Theme Gallery”, “Web Part Gallery”, “wfpub”)

Write-Host  “Total number of webs that will be traversed: ” $webs.count

$DocLibsCount = 0
$DocLibwItems = 0
$totalitems = 0
$subfolderitems = 0

foreach($web in $webs)
    {
        $listcoll = $web.lists
       
        foreach($list in $listcoll)
        {
             if($list -eq $null)
             {
                Write-Host
             }
             else
             {
                $base = $list.GetType()
                if($base.name -eq “SPDocumentLibrary”)
                {
                    if ($systemlibs -contains $list)
                    { continue}
                    else
                        { 
                          $DocLibsCount += 1   
                          $items = $list.items
                         
                          if($items -ne “0”)
                          {
                           $DocLibwItems += 1
                           Write-Host “Processing ItemCount for DobLib ” $DocLibsCount -ForegroundColor Red
                           $totalitems  += $items.count
                  
                           $name = $list.Title
                           $folders  = $web.GetFolder($name).SubFolders
                           for($etr = 0;$etr -lt $folders.count; $etr++)
                            {
                             if($folders[$etr].Name -ne “Forms”)
                               {
                                 Write-Host “Processing SubFolder ItemCount” -ForegroundColor Red
                                 $tempcount = $folders[$etr].ItemCount
                                 $subfolderitems += $tempcount
                                 }
                            }
                           }
                          }
                   }
                 }
              }
          } 
 
Write-Host
Write-Host
Write-Host “Total # of Document Libraries: ” $DocLibsCount -ForegroundColor Green
Write-Host “Total # of Document Libraries that contain items: ” $DocLibwItems -ForegroundColor Green
Write-Host “Total # of items: ” $totalitems -ForegroundColor Green
Write-Host “Total # of items in DocLib\Subfolders: ” $subfolderitems -ForegroundColor Green
$finishtime = Get-Date
Write-Host
Write-Host “Script Duration” –ForegroundColor Yellow
Write-Host “Started:   “ $starttime –ForegroundColor Yellow
Write-Host “Finished: “ $finishtime –ForegroundColor Yellow

Stop-SPAssignment -Global

 

Thanks,

Russ Maxwell, MSFT   School bus