Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Powershell Script For Getting The Items Count Which Are Added In Last 30 Days
WhatsApp
Prasham Sabadra
Aug 14
2016
2.2
k
0
0
<#
.SYNOPSIS
Can be used to loop through all lists available across the site collection, from all sites and their item counts which are added
in
last 30 days within SharePoint Online tenant. We are writting list title, relative url of list and last 30 days item count
in
.csv file
.EXAMPLE
PS C:\> .\Get-Last30DaysItemCountFromAllListInSiteCollection.ps1 -SiteCollectionUrl https:
//MySiteCollection.sharepoint.com
.EXAMPLE
PS C:\> $creds = Get-Credential
PS C:\> .\Get-Last30DaysItemCountFromAllListInSiteCollection.ps1 -SiteCollectionUrl https:
//contoso-admin.sharepoint.com -Credentials $creds
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory = $
true
, HelpMessage=
"Enter the URL of the target site collection , e.g. 'https://contoso.sharepoint.com'"
)]
[String]
$SiteCollectionUrl,
[Parameter(Mandatory = $
false
, HelpMessage=
"Optional administration credentials to site collection."
)]
[PSCredential]
$Credentials
)
#Looping through all the webs and all their sub webs. Calling this function recursively.
function Loop-AllWebs{
Param(
[Microsoft.SharePoint.Client.ClientContext]$Context,
[Microsoft.SharePoint.Client.Web]$RootWeb
)
#Getting all webs
$Webs = $RootWeb.Webs
$Context.Load($Webs)
$Context.ExecuteQuery()
ForEach ($sWeb
in
$Webs)
{
Get-AllListsDetails -Context $Context -web $sWeb
#Looping through all sub webs
Loop-AllWebs -RootWeb $sWeb -Context $Context
}
}
function Get-AllListsDetails{
Param(
[Microsoft.SharePoint.Client.ClientContext]$Context,
[Microsoft.SharePoint.Client.Web]$web
)
$lists=$web.Lists
$Context.Load($lists)
$Context.ExecuteQuery()
foreach
($list
in
$lists)
{
#Getting view for getting the list URL
$view = $list.DefaultView
$spQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
#CAML Query Using a DateTime Value and and Offset of Today
$query =
'<View><Query><Where><Geq><FieldRef Name="Created" /><Value Type="DateTime"><Today OffsetDays="-30" /></Value></Geq></Where></Query></View>'
$spQuery.ViewXml = $query
$spListItemCol = $list.GetItems($spQuery)
$Context.Load($list)
$Context.Load($view)
$Context.Load($spListItemCol)
$Context.ExecuteQuery()
$line = $list.Title +
","
+ $view.ServerRelativeUrl +
","
+ $spListItemCol.Count
#Writting to CSV file
Add-Content Result.csv $line #Give the full path where you want to store the result. Currently it will be generated at the location of
this
script
}
}
# Get reference to SPO CSOM assemblies
Add-Type -Path
"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path
"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Write-Host -ForegroundColor White
"---------------------------------------------------------------------------"
Write-Host -ForegroundColor White
"Get All Lists across Site Collection and last 30 days Items counts from each and every lists "
Write-Host -ForegroundColor White
"---------------------------------------------------------------------------"
Write-Host -ForegroundColor Yellow
"Site Collection URL: $SiteCollectionUrl"
Write-Host
""
Write-Host
""
# Get credentials, if they were not provided
if
($Credentials -eq $
null
)
{
$Credentials = Get-Credential -Message
"Enter Admin Credentials"
}
[Microsoft.SharePoint.Client.ClientContext]$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($SiteCollectionUrl)
$clientContext.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Credentials.UserName, $Credentials.Password)
$web = $clientContext.Web
$clientContext.Load($web)
$clientContext.ExecuteQuery()
Loop-AllWebs -RootWeb $web -Context $clientContext
Powershell Script
Items Count
Up Next
Powershell Script For Getting The Items Count Which Are Added In Last 30 Days