Skip to main content

PowerShell script to count number of list and library items in a site for SharePoint Online and MOSS 2007

This below script mainly helps to get the site item count for both SharePoint Online and MOSS 2007. This will be useful when you are migrating site from MOSS2007 to SharePoint online.
Script requires input csv file which contains list of site url as below

Input.CSV

SiteUrl


when you execute the script, you need to provide 2 input parameters
'Enter input csv path with file name'   - c:\script\input.csv
Enter Output Path- c:\script\outpu.csv


Script for SharePoint 2007
#clear the PowerShell window
cls

 $CsvPath=Read-Host -Prompt 'Enter input csv path with file name'
 $ReportPath=Read-Host -Prompt 'Enter Output Path'
# Exclude the following libaries / lists
$excludeLists = @("Master Page Gallery",
                "User Information List",
                "TaxonomyHiddenList",
                "Theme Gallery",
                "Style Library",
                "Solution Gallery",
                "Reusable Content",
                "Reporting Templates",
                "Reporting Metadata",
                "Long Running Operation Status",
                "List Template Gallery",
                "Content and Structure Reports",
                "Cache Profiles",
                "wfpub",
                "Workflows",
                "Workflow History",
                "Web Part Gallery",
                "Form Templates",
                "Workflow Tasks",
                "Suggested Content Browser Locations",
                "Site Template Gallery",
                "fpdatasources",
                "Variation Labels",
                "Content type publishing error log",
                "Converted Forms"
                )

#Load the PowerShell commandlets for SharePoint using PowerShell 1.0 and MOSS 2007 compatibility
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

 $csvInput= Import-Csv $CsvPath
#Print the header in the resulting file
$Header = "Source URL`tSource file count`tsource library count"
Out-File -inputobject $Header -filepath $ReportPath -Force

function get-spweb ([String]$webUrl=$(throw 'Parameter -webUrl is missing!'))
{
   $site =  New-Object -TypeName "Microsoft.SharePoint.SPSite" -ArgumentList "$webUrl";
   return $site.OpenWeb();
}

 Foreach($item in $csvInput)
{
     $web = get-spweb $item.SiteUrl
             $Lists = $web.Lists

             $Itemcount=0
             $listCount-0;

            foreach ($list in $Lists)
            {
                if ($excludeLists -notcontains $list.Title)
                {
                    #Get the count of items in the current list
                    #$curListitemCount = $list.Folders.Count + $list.Items.Count
                   $listCount++;
                   $Itemcount=$Itemcount+$list.Items.Count
                }
            }
           $Result = $web.URL +"`t" +$Itemcount +"`t"+ $listCount
            Out-File -inputobject $Result -filepath $ReportPath -append           
           
            $web.Dispose()
}
    


 Script for SharePoint online

# replace these details (also consider using Get-Credential to enter password securely as script runs)..


$username = Read-Host -Prompt 'Enter username'
$password = Read-Host -Prompt 'Enter password'
$CsvPath=Read-Host -Prompt 'Enter input csv path with file name'
$ReportPath=Read-Host -Prompt 'Enter Output Path'


$securePassword = ConvertTo-SecureString $Password -AsPlainText -Force

# the path here may need to change if you used e.g. C:\Lib..
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
# note that you might need some other references (depending on what your script does) for example:
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll"



#CSV
$csvInput= Import-Csv $CsvPath
$Header = "Source URL`tSource file count`tsource library count"
Out-File -inputobject $Header -filepath $ReportPath -Force

#Out-File -inputobject $Header -filepath $ReportPath -Force
$excludeLists = @("App Packages",
                    "appdata",
                    "appfiles",
                    "Apps in Testing",
                    "Cache Profiles",
                    "Composed Looks"
                    "Content and Structure Reports",
                    "Content type publishing error log",
                    "Converted Forms",
                    "Customer Form",
                    "DB",
                    "Device Channels",                    
                    "Form Templates",
                    "fpdatasources",
                    "Get started with Apps for Office and SharePoint",
                    "List Template Gallery",
                    "Long Running Operation Status",
                    "Maintenance Log Library",
                    "Master Docs",
                    "Master Page Gallery",
                    "MicroFeed",
                    "NintexFormXml",                  
                    "Quick Deploy Items",
                    "Relationships List",
                    "Reusable Content",
                    "Search Config List",
                    "Site Assets",
                    "Site Collection Documents",
                    "Site Collection Images",
                    "Site Pages",
                    "Solution Gallery",
                    "Style Library",
                    "Suggested Content Browser Locations",                
                    "TaxonomyHiddenList",
                    "Web Part Gallery",
                    "wfpub",
                    "wfsvc",
                    "Workflow History",
                    "Workflow Tasks")
Foreach($item in $csvInput)
{

# connect/authenticate to SharePoint Online and get ClientContext object..
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($item.SiteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials

 $lists = $clientContext.Web.Lists
 $clientContext.Load($lists)
 $clientContext.ExecuteQuery()

     $ItemCount=0
      $listCount=0;
     foreach($list in $lists)
     {
         if($excludeLists -notcontains $list.Title)
           {
               $ItemCount=$ItemCount+$list.ItemCount
                $listCount++;
              
           }
  
     }
     $Result = $item.SiteUrl +"`t" +$Itemcount +"`t"+ $listCount
     Out-File -inputobject $Result -filepath $ReportPath -append

}








Comments

ManiGandan K said…
I need to get the could for wiki and blog library, could you please help me on this.
ManiGandan K said…
I need to get the count for wiki and blog library, could you please help me on this.

Popular posts from this blog

Download User Profile Pictures from SharePoint 2010 - CSOM

In this post I am trying to explain how we can download UserProfie pictures from SharePoint 2010 on premise environment using CSOM code Two input files: CSV file contains list of users and Email ids(csvInput.csv) Xml input file contains input parameters(InputXml.xml) CSV File format: User ID,First Name,Surname,E-mail, 1233,veera,induvasi,veera@abc.com, 1333,mahesh,gupta,mg@abc.com, 2345,abc,azx,abc@abc.com, InputXml File format: < Element >   < InputDetails >     < siteUrl > http://abc.com </ siteUrl >     < ExportImageLocation > c: \Download </ ExportImageLocation >     < InputCSVFullPath > c:\UserProfile\csvInput .csv </ InputCSVFullPath >     < loginName >create.idea @abc.com </ loginName >     < Password > ##### </ Password >   </ InputDetails > </ Element > Xml parameter Details: < siteUrl> - SharePoint site url < ExportImage

Update Choice Field values through powerShell

Scenario: updating choice column option value for all the columns in a site collection including  sub sites. Solution ; Below script will iterate through all the sites and sub sites of site collection and finds the  choice column " choiceColumnName" and  will change the value "Choice 1" to "Option 1". Powerhsell Script: param($url = $(Read-Host -prompt "Root Site Collection Url")) #Get the PowerShell Snapin if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {     Add-PSSnapin "Microsoft.SharePoint.PowerShell" } #Get Root Site $root = Get-SPSite $url #If site was found / valid if($root -ne $null) {      foreach($subSite in $root.AllWebs)      {                  $subSiteTitle = $subSite.Title                 Write-Host $subSiteTitle -ForegroundColor Magenta                 $subSiteURL = $subSite.Url                 Write-Host $subSiteURL -Foregroun