Please help - Error: Cannot index into a null array.

#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 get all files of a folder
Function Get-AllFilesFromFolder([Microsoft.SharePoint.Client.Folder]$Folder)
{
#Get All Files of the Folder
$Ctx = $Folder.Context
$Ctx.load($Folder.files)
$Ctx.ExecuteQuery()

#Get all files in Folder
<# ForEach ($File in $Folder.files)
{
#Get the File Name or do something
Write-host -f Green $File.Name
} #>
$DataCollection = @()
#Iterate through each document in the library
ForEach($File in $Folder.files)
{
#Collect data
$Data = New-Object PSObject -Property ([Ordered] @{
FileName = $File.FieldValues["FileLeafRef"]
RelativeURL = $File.FieldValues["FileRef"]
CreatedBy = $File.FieldValues["Created_x0020_By"]
CreatedOn = $File.FieldValues["Created"]
ModifiedBy = $File.FieldValues["Modified_x0020_By"]
ModifiedOn = $File.FieldValues["Modified"]
FileSize = $File.FieldValues["File_x0020_Size"]
})
$DataCollection += $Data
}
$DataCollection

#Export Documents data to CSV
$DataCollection | Export-Csv -Path $CSVPath -Force -NoTypeInformation
Write-host -f Green "Documents Data Exported to CSV!"

#Recursively Call the function to get files of all folders
$Ctx.load($Folder.Folders)
$Ctx.ExecuteQuery()

#Exclude "Forms" system folder and iterate through each folder
ForEach($SubFolder in $Folder.Folders | Where {$_.Name -ne "Forms"})
{
Get-AllFilesFromFolder -Folder $SubFolder
}
}

#powershell list all documents in sharepoint online library
Function Get-SPODocumentLibraryFiles()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $LibraryName,
[Parameter(Mandatory=$true)] [System.Management.Automation.PSCredential] $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 Library and Its Root Folder
$Library=$Ctx.web.Lists.GetByTitle($LibraryName)
$Ctx.Load($Library)
$Ctx.Load($Library.RootFolder)
$Ctx.ExecuteQuery()

#Call the function to get Files of the Root Folder
Get-AllFilesFromFolder -Folder $Library.RootFolder
}
Catch {
write-host -f Red "Error:" $_.Exception.Message
}
}
#Config Parameters

$SiteURL= "https://xxxxxxxxxx/sites/xxxxxxxx/"
$LibraryName="XX Project Archive"

$CSVPath = "C:\Lakshmi\TEST\ARCHIVE.csv"


#Get Credentials to connect
$Cred = Get-Credential

#Call the function to Get All Files from a document library
Get-SPODocumentLibraryFiles -SiteURL $SiteURL -LibraryName $LibraryName -Credential $Cred

If you run your code line by line when do you get the error? What validation have you done? What is the exact error message you receive a copy and paste of it will help us to identify the issue.

@lpatange - It’ll be more easy if you could post the complete error you get here. Don’t forget to use the code formatting tags :slight_smile:

https://powershell.org/forums/topic/read-me-before-posting-youll-be-glad-you-did/