SharePoint Online:使用 PowerShell 获取文档库清单(文件夹-子文件夹-文件结构)
要求:获取 SharePoint 文档库的文件文件夹结构并将其导出为 CSV。
如何获取 SharePoint Online 文档库中所有文件和文件夹的列表?
如果您需要获取 SharePoint Online 文档库中所有文件和文件夹的列表,可以使用几种不同的方法来实现。也许您需要清理您的图书馆,或者只是对里面有什么感到好奇。在本文中,我们将向您展示如何获取文档库中所有文件和文件夹的列表,以及如何使用 PowerShell 获取所需的信息。让我们开始吧!
在 SharePoint Online 中,可以轻松生成文档清单报告以获取库中所有文档的概述。您可以使用 SharePoint Online 文档库中的“导出到 Excel”功能(而不是“导出到 CSV”——仅导出视图中的文件和文件夹)来获取库中所有文件和文件夹的清单。只需导航到文档库,然后单击命令栏中的“导出到 Excel”按钮即可导出 SharePoint Online 文档库中的所有文件和文件夹列表及其元数据。
这将下载一个 .iqy 文件,并在 Excel 中打开该文件为我们提供报告:
这对于故障排除或快速概览库的内容非常有用。
SharePoint Online:用于获取文档库清单的 PowerShell
您是否正在寻找一种方法来获取 SharePoint Online 文档库中存储的所有文档的清单?嗯,这是可以提供帮助的方便的 PowerShell!在这篇博文中,我们将向您展示如何使用 PowerShell 生成列出所有文档及其属性的报告,以便您可以跟踪库中存储了哪些文档。
让我们将所有文件夹、子文件夹和文件结构从 SharePoint Online 文档库导出到 CSV 文件:
#Load SharePoint 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"
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Documents"
$CSVPath = "C:\Temp\DocumentLibraryRpt.csv"
$BatchSize = 500
#Get 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 Document Library
$List =$Ctx.Web.Lists.GetByTitle($ListName)
#Define CAML Query to Get List Items in batches
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml ="
<View Scope='RecursiveAll'>
<Query>
<OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy>
</Query>
<RowLimit Paged='TRUE'>$BatchSize</RowLimit>
</View>"
$DataCollection = @()
Do
{
#get List items
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
#Iterate through each item in the document library
ForEach($ListItem in $ListItems)
{
#Collect data
$Data = New-Object PSObject -Property ([Ordered] @{
Name = $ListItem.FieldValues.FileLeafRef
RelativeURL = $ListItem.FieldValues.FileRef
Type = $ListItem.FileSystemObjectType
CreatedBy = $ListItem.FieldValues.Author.Email
CreatedOn = $ListItem.FieldValues.Created
ModifiedBy = $ListItem.FieldValues.Editor.Email
ModifiedOn = $ListItem.FieldValues.Modified
FileSize = $ListItem.FieldValues.File_x0020_Size
})
$DataCollection += $Data
}
$Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
}While($Query.ListItemCollectionPosition -ne $null)
#Export Documents data to CSV
$DataCollection | Export-Csv -Path $CSVPath -Force -NoTypeInformation
Write-host -f Green "Document Library Inventory Exported to CSV!"
}
Catch {
write-host -f Red "Error:" $_.Exception.Message
}
使用 PnP PowerShell 获取文档库中的文件文件夹结构
同样,如果要将文档库的所有文件文件夹结构导出到 CSV 文件,请使用以下 PnP PowerShell 脚本:
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/RI/"
$ListName= "Non-Transaction"
$ReportOutput = "C:\Temp\ListInventory.csv"
#Connect to SharePoint Online site
Connect-PnPOnline $SiteURL -Interactive
#Array to store results
$Results = @()
#Get all Items from the document library
$List = Get-PnPList -Identity $ListName
$ListItems = Get-PnPListItem -List $ListName -PageSize 500
Write-host "Total Number of Items in the List:"$List.ItemCount
$ItemCounter = 0
#Iterate through each item
Foreach ($Item in $ListItems)
{
$Results += New-Object PSObject -Property ([ordered]@{
Name = $Item["FileLeafRef"]
Type = $Item.FileSystemObjectType
FileType = $Item["File_x0020_Type"]
RelativeURL = $Item["FileRef"]
CreatedBy = $Item["Author"].Email
CreatedOn = $Item["Created"]
})
$ItemCounter++
Write-Progress -PercentComplete ($ItemCounter / ($List.ItemCount) * 100) -Activity "Processing Items $ItemCounter of $($List.ItemCount)" -Status "Getting Metadata from Item '$($Item['FileLeafRef'])"
}
#Export the results to CSV
$Results | Export-Csv -Path $ReportOutput -NoTypeInformation
Write-host "Document Library Inventory Exported to CSV Successfully!"
以及脚本的输出报告:
总之,通过使用 PowerShell 脚本可以轻松实现从 SharePoint Online 文档库检索所有文档,如上所示。此脚本提供了 SharePoint 文档库中存储的内容的全面概述,包括有关文件类型、所有者和上次修改日期的信息。


