Skip to content

Instantly share code, notes, and snippets.

@berteh
Created August 18, 2023 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save berteh/1dce9e9d4431d0902542100655091844 to your computer and use it in GitHub Desktop.
Save berteh/1dce9e9d4431d0902542100655091844 to your computer and use it in GitHub Desktop.
PWS list files in sharepoint document repository
#inspired from
# https://www.sharepointdiary.com/2018/03/sharepoint-online-get-all-documents-using-powershell.html
# https://www.sharepointdiary.com/2018/03/sharepoint-online-powershell-to-get-folder-in-document-library.html
#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"
#Function to Generate Report on all documents in a SharePoint Online Site Collection
Function Get-SPODocumentInventory($SiteURL)
{
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the web from given URL
$Web = $Ctx.web
$Ctx.Load($Web)
$Ctx.executeQuery()
Write-host -f Yellow "Connected to Site: $SiteURL"
#Get the Folder object by Server Relative URL
$Root = $Web.GetFolderByServerRelativeUrl($ServerRelativeUrl)
$Ctx.Load($Root)
$Ctx.ExecuteQuery()
Write-host -f Yellow "Got to directory : $ServerRelativeUrl"
#Filter Document Libraries to Scan
$Ctx.Load($Root.Folders)
$Ctx.ExecuteQuery()
Write-host -f Cyan "`t Loaded root directory $($Root.Name) with $($Root.ItemCount) Item(s)"
#Write-Output $Root.Folders
#$Lists = $Folder.Lists | Where {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $false -and $SystemLists -notcontains $_.Title -and $_.ItemCount -gt 0}
#Loop through each Folder
Foreach ($Folder in $Root.Folders)
{
Write-host -f Cyan "`t Processing Folder $($Folder.Name) with $($Folder.ItemCount) Item(s)"
#Iterate through each file and get data
$Ctx.Load($Folder.Files)
$Ctx.ExecuteQuery()
$DocumentInventory = @()
Foreach($File in $Folder.Files)
{
#Write-host "`t new File : $($File.Name)"
$DocumentData = New-Object PSObject
$DocumentData | Add-Member NoteProperty SiteURL($SiteURL)
$DocumentData | Add-Member NoteProperty FileURL($File.ServerRelativeUrl)
$DocumentData | Add-Member NoteProperty Name($File.Name)
#Add the result to an Array
$DocumentInventory += $DocumentData
}
#Export the result to CSV file
$DocumentInventory | Export-CSV $ReportOutput -NoTypeInformation -Append
}
}
Catch {
write-host -f Red "Error Generating Document Inventory!" $_.Exception.Message
}
}
#Config Parameters
#$SiteCollURL="https://crescent.sharepoint.com/sites/marketing"
$SiteCollURL="https://colabor01.sharepoint.com/sites/Alimentations"
$ServerRelativeUrl= "/sites/Alimentations/Documents%20partages/Mara%C3%AEchage/Stunden/Demande%20de%20cong%C3%A9%202023"
$ReportOutput="C:\temp\DemandesConges-scans.csv"
$BatchSize = 500
#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Delete the Output Report, if exists
if (Test-Path $ReportOutput) { Remove-Item $ReportOutput }
#Call the function
Get-SPODocumentInventory $SiteCollURL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment