Hello there,
Debugging a Windows PowerShell script often involves setting a breakpoint, which is something that causes the Windows PowerShell script to pause execution. When the script pauses execution, the Windows PowerShell console drops into debug mode. You can try to debug your script by following this article https://devblogs.microsoft.com/scripting/use-the-powershell-debugger-to-troubleshoot-scripts/
Meanwhile, the below PowerShell script gets all document libraries from the given site URL, along with the library URL and a number of items in each library.
#Set Variables
$SiteURL = "https://abc"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get all document libraries - Exclude Hidden Libraries
$DocumentLibraries = Get-PnPList | Where-Object {$_.BaseTemplate -eq 101 -and $_.Hidden -eq $false} #Or $_.BaseType -eq "DocumentLibrary"
#Get Document Libraries Name, Default URL and Number of Items
$DocumentLibraries | Select Title, DefaultViewURL, ItemCount
Hope this resolves your Query !!
--If the reply is helpful, please Upvote and Accept it as an answer–
PnP.Powershell to retrieve all documents from SharePoint Online sites
I am trying to retrieve all documents from SharePoint online sites and export them to their own CSV file. The code works fine when explicitly stating the site URL in the original parameters. However it does not work when I try to import a list of sites from a CSV file and run it using a nested foreach statement. It does not process through the #Iterate through each site foreach statement.
I want to loop through all the sites in the CSV file, output all the documents and export the results to individual files using the Name column in the CSV as its filename.
Any help would be appreciate, thanks.
Original:
#Parameters
$SiteURL = "https://org.sharepoint.com/sites/TestSite1"
$ListName= "Documents"
$ReportOutput = "C:\Temp\TestSite1.csv"
$Pagesize = 2000
#Connect to SharePoint Online site
Connect-PnPOnline $SiteURL -Interactive
Modified: added #Iterate through each site
CSV input file is
Name URL
Site1 https://org.sharepoint.com/sites/Site2
Site2 https://org.sharepoint.com/sites/Site2
#Parameters
$SP = "https://org.sharepoint.com"
$SiteList = Import-Csv -Path "\Sites2-test.csv"
$SitePath = $SiteList.URL
$FileName = $SiteList.Name
$ListName= "Documents"
$Pagesize = 2000
$ReportOutput = "C:\Temp\Site-AllDocs_"+$FileName+".csv"
#Connect to SharePoint Online site
Connect-PnPOnline $SP -Interactive
#Iterate through each site
ForEach ($Site in $SitePath)
{
#Array to store results
$Results = @()
#Get all Documents from the document library
$List = Get-PnPList -Identity $ListName
$global:counter = 0;
$ListItems = Get-PnPListItem -List $ListName -PageSize $Pagesize -Fields Author, Editor, Created, File_x0020_Type -ScriptBlock `
{ Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete ($global:Counter / ($List.ItemCount) * 100) -Activity `
"Getting Documents from Library '$($List.Title)'" -Status "Getting Documents data $global:Counter of $($List.ItemCount)";} | Where {$_.FileSystemObjectType -eq "File"}
$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"]
CreatedByEmail = $Item["Author"].Email
CreatedOn = $Item["Created"]
Modified = $Item["Modified"]
ModifiedByEmail = $Item["Editor"].Email
})
$ItemCounter++
Write-Progress -PercentComplete ($ItemCounter / ($List.ItemCount) * 100) -Activity "Exporting data from Documents $ItemCounter of $($List.ItemCount)" -Status "Exporting Data from Document '$($Item['FileLeafRef'])"
}
#Export the results to CSV
$Results | Export-Csv -Path $ReportOutput -NoTypeInformation
}
Write-host "Document Library Inventory Exported to CSV Successfully!"
Microsoft 365 and Office SharePoint For business Windows
Windows for business Windows Server User experience PowerShell
1 answer
Sort by: Most helpful
-
Limitless Technology 44,751 Reputation points
2023-05-04T13:29:32.69+00:00