Last active
June 19, 2023 08:55
-
-
Save AshFlaw/a46fdb46a92c96c3420f1d9ab0bc0122 to your computer and use it in GitHub Desktop.
SharePoint Online: Get an inventory of files and folders from a document library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-CSOMDocumentLibraryInventory { | |
param ( | |
[Parameter(Mandatory=$True)] | |
[string]$SiteURL, | |
[Parameter(Mandatory=$True)] | |
[string]$LibraryName, | |
[int]$BatchSize, | |
[Parameter(Mandatory=$True)] | |
[System.Management.Automation.PSCredential] | |
$Credential | |
) | |
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" | |
if ($null -eq $BatchSize) { | |
$BatchSize = 500 | |
} | |
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) | |
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Credential.UserName,$Credential.Password) | |
$List =$Ctx.Web.Lists.GetByTitle($LibraryName) | |
$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 { | |
$ListItems = $List.GetItems($Query) | |
$Ctx.Load($ListItems) | |
$Ctx.ExecuteQuery() | |
foreach ($ListItem in $ListItems) { | |
$Data = New-Object PSObject -Property ([Ordered] @{ | |
SiteURL = $SiteURL | |
LibraryName = $LibraryName | |
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) | |
$DataCollection | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment