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