Skip to content

Instantly share code, notes, and snippets.

@ChendrayanV
Created November 16, 2023 12:58
Show Gist options
  • Save ChendrayanV/9549fefc138a301f711a21158ff5a9a7 to your computer and use it in GitHub Desktop.
Save ChendrayanV/9549fefc138a301f711a21158ff5a9a7 to your computer and use it in GitHub Desktop.
SharePoint Document Library Inventory
param (
$SiteUrl,
$DocumentLibraryName,
$BatchSize = 500
)
Import-Module ""
Import-Module ""
$Username = ""
$Password = "" | ConvertTo-SecureString -AsPlainText -Force
$ClientContext = [Microsoft.SharePoint.Client.ClientContext]::new($SiteUrl)
$ClientContext.Credentials = [Microsoft.SharePoint.Client.SharePointOnlineCredentials]::new($Username, $Password)
$List = $ClientContext.Web.Lists.GetByTitle($DocumentLibraryName)
$ClientContext.Load($ListCollection)
$ClientContext.ExecuteQuery()
$Query = [Microsoft.SharePoint.Client.CamlQuery]::new()
$Query.ViewXml = @"
<View Scope='RecursiveAll'>
<Query>
<OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy>
</Query>
<RowLimit Paged='TRUE'>$BatchSize</RowLimit>
</View>
"@
$ResultCollection = @()
Do {
$ListItems = $List.GetItems($Query)
$ClientContext.Load($ListItems)
$ClientContext.ExecuteQuery()
ForEach ($ListItem in $ListItems) {
#Collect data
$ResultCollection += [PSCustomObject]@{
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
}
}
$Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
} While ($null -ne $Query.ListItemCollectionPosition)
$ResultCollection | Export-Csv .\ResultCollection.csv -NoTypeInformation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment