[玩转系统] SharePoint Online:使用 PowerShell 获取列表项计数
作者:精品下载站 日期:2024-12-14 21:14:32 浏览:12 分类:玩电脑
SharePoint Online:使用 PowerShell 获取列表项计数
要求:使用 PowerShell 获取 SharePoint Online 中的列表项计数。
如何获取 SharePoint Online 中的列表项计数?
如果您需要获取给定 SharePoint Online 列表中的项目计数,有几种方法可以实现。在本文中,我们将向您展示如何使用 PowerShell-CSOM 获取任何 SharePoint Online 列表的项目计数。我们还将向您展示如何使用 PnP PowerShell 获取列表项计数。如果您必须生成包含此信息的报告,这会很有用。让我们开始吧!
SharePoint Online 中的“网站内容”页面为您提供每个列表和库中的项目数量,以及名称、类型和上次修改日期信息。 (网址:https://.sharepoint.com/_layouts/Viewlsts.aspx)
SharePoint Online:用于获取列表项计数的 PowerShell
您是否希望在 SharePoint Online 中使用 PowerShell 获取列表项计数?使用此 CSOM PowerShell 脚本查找 SharePoint Online 列表中的项目数:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Documents"
#Setup Credentials to connect
$Cred = Get-Credential
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
#Get the List
$List= $Ctx.web.lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.ExecuteQuery()
#Get List Item Count
Write-host -f Green "Total Number of Items in the List:"$List.ItemCount
}
Catch {
write-host -f Red "Error Getting List Item Count!" $_.Exception.Message
}
让我们将上面的脚本包装成一个可重用的函数,并获取列表或文档库的列表项计数。
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Function Get-SPOListItemCount{
<#
.SYNOPSYS
Returns the Item Count for the specified list or library
.DESCRIPTION
Returns the Item Count for the specified list or library
.PARAMETER WebUrl
The URL of the site that contains the list or library
.PARAMETER ListName
The title of the list or library to get count
.EXAMPLE
Get-SPOListItemCount -WebUrl "https://tenant.sharepoint.com/teams/marketing" -ListName "Shared Documents"
#>
[CmdletBinding()]
[OutputType([int])]
Param
(
[Parameter(Mandatory=$true, HelpMessage="The URL of the site that contains the list or library", Position=0)] [string]$WebUrl,
[Parameter(Mandatory=$true, HelpMessage="The Title of the list or library", Position=1)] [string]$ListName
)
Begin{
#Get Credentials to connect
$Credential = Get-Credential
#Setup context to connect
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($WebUrl)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Credential.UserName, $Credential.Password)
}
Process{
try{
#Get the Web
$Web = $Ctx.Web
#Get the List
$List = $web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.ExecuteQuery()
#sharepoint online count items in list
Return $List.ItemCount
}
catch{
Write-Host -ForegroundColor Red $_.Exception.Message
return 0
}
}
End{
$Ctx.Dispose()
}
}
#Call the function to get item count
Get-SPOListItemCount -WebUrl "https://crescent.sharepoint.com/sites/marketing" -ListName "Documents"
此 PowerShell 脚本获取 SharePoint Online 中的列表项计数
使用 PnP PowerShell 获取 SharePoint Online 中的列表项计数
让我们从 SharePoint Online 网站的所有列表中获取一些项目。如果您需要准确计算库中的文件数量以用于报告或其他目的,这会很有用。
#Variable
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"
#Connect to PnP Online
Connect-PnPOnline $SiteURL -Credentials (Get-Credential)
#Get List Item count from all Lists from the Web
Get-PnPList | Select Title, ItemCount
您还可以使用此 PowerShell 脚本对 SharePoint Online 文档库中的项目进行计数。同样,您可以根据特定条件过滤列表项并获取其计数:
#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Tasks"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#Get list items - Filtered by given condition
$ListItems = Get-PnPListItem -List $ListName -Query "<View><Query><Where><Eq><FieldRef Name='Status'/><Value Type='Text'>Completed</Value></Eq></Where></Query></View>"
#Get List Items Count
$ListItems.Count
如何导出网站集中所有列表的列表项计数?
以下介绍了如何使用 PnP PowerShell 将网站集中的所有列表和库中的项目计数导出为 CSV 格式。
#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$CSVFile = "C:\temp\ListItemCount.csv"
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get All Webs from the site collection
$SubWebs = Get-PnPSubWeb -Recurse -IncludeRootWeb
$ListInventory= @()
Foreach ($Web in $SubWebs)
{
Write-host -f Yellow "Getting List Item count from site:" $Web.URL
#Connect to Subweb
Connect-PnPOnline -Url $Web.URL -Interactive
#Get all lists and libraries of the Web
$ExcludedLists = @("Reusable Content","Content and Structure Reports","Form Templates","Images","Pages","Workflow History","Workflow Tasks", "Preservation Hold Library")
$Lists= Get-PnPList | Where {$_.Hidden -eq $False -and $ExcludedLists -notcontains $_.Title}
foreach ($List in $Lists)
{
$Data = new-object PSObject
$Data | Add-member NoteProperty -Name "Site Name" -Value $Web.Title
$Data | Add-member NoteProperty -Name "Site URL" -Value $Web.Url
$Data | Add-member NoteProperty -Name "List Title" -Value $List.Title
$Data | Add-member NoteProperty -Name "List URL" -Value $List.RootFolder.ServerRelativeUrl
$Data | Add-member NoteProperty -Name "List Item Count" -Value $List.ItemCount
$ListInventory += $Data
}
}
$ListInventory | Export-CSV $CSVFile -NoTypeInformation
Write-host -f Green "List Inventory Exported to Excel Successfully!"
}
Catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
结果 :
如果您想统计文档库中的文件数量怎么办?
SharePoint Online:使用 PowerShell 对文档库中的项目进行计数
由于 SharePoint Online 列表的 ItemCount 属性为您提供项目计数,包括文件和文件夹,因此我们如何对库中的文件和文件夹进行计数?好吧,这是获取文件和文件夹数量的 PowerShell:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Documents"
#Setup Credentials to connect
$Cred = Get-Credential
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
#Get the List
$List= $Ctx.web.lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.ExecuteQuery()
#Define Query to Filter and Get All Files from the list
$Query = "@
<View Scope='RecursiveAll'>
<Query>
<Where>
<Eq>
<FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value>
</Eq>
</Where>
</Query>
</View>"
$FilesQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
$FilesQuery.ViewXml =$Query
$Files = $List.GetItems($FilesQuery)
$Ctx.Load($Files)
$Ctx.ExecuteQuery()
#Define Query to Filter and Get All Folders from the list
$Query = "@
<View Scope='RecursiveAll'>
<Query>
<Where>
<Eq>
<FieldRef Name='FSObjType' /><Value Type='Integer'>1</Value>
</Eq>
</Where>
</Query>
</View>"
$FoldersQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
$FoldersQuery.ViewXml =$Query
$Folders = $List.GetItems($FoldersQuery)
$Ctx.Load($Folders)
$Ctx.ExecuteQuery()
#Get List Item Count
Write-host -f Green "Total Number of Items in the List:"$List.ItemCount
Write-host -f Green "Total Number of Files in the List:"$Files.Count
Write-host -f Green "Total Number of Folders in the List:"$Folders.Count
}
Catch {
write-host -f Red "Error Getting List Item Count!" $_.Exception.Message
}
您还可以循环遍历列表项并检查 $ListItem.FileSystemObjectType -eq “File” 或 “Folder” 以获取文件和文件夹计数。这是我在 SharePoint Online 网站 SharePoint Online 中获取大型列表的另一篇文章:如何使用 PowerShell 查找所有大型列表?
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag