In today’s tutorial, I will show you how to fix an error: “An error occurred while enumerating through a collection: The collection has not been initialized.”, that comes while working with PnP PowerShell.
An error occurred while enumerating through a collection: The collection has not been initialized.
Recently, I was working on a PowerShell script to check if a field exists in a SharePoint Online document library. I write the code below:
Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/PowerShellFAQs" -Interactive
$list = Get-PnPList -Identity "EmployeeRecords"
$fieldExists = $list.Fields | Where-Object { $_.InternalName -eq "EmployeeID" }
if ($fieldExists) {
Write-Output "Field 'EmployeeID' exists in the list 'EmployeeRecords'."
} else {
Write-Output "Field 'EmployeeID' does not exist in the list 'EmployeeRecords'."
}There, I got the error that says:
“An error occurred while enumerating through a collection: The collection has not been initialized. It has not been requested, or the request has not been executed. It may need to be explicitly requested..“. You can see the error screenshot below that I took from the Visual Studio code.

Solution
Now, let me show you the code that I write to fix the above error.
Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/PowerShellFAQs" -Interactive
$listName = "EmployeeRecords"
$fieldName = "EmployeeID"
# Get the list
$list = Get-PnPList -Identity $listName
# Load the fields collection
$ctx = Get-PnPContext
$ctx.Load($list.Fields)
$ctx.ExecuteQuery()
# Check if the field exists
$fieldExists = $list.Fields | Where-Object { $_.InternalName -eq $fieldName }
if ($fieldExists) {
Write-Output "Field 'EmployeeID' exists in the list 'EmployeeRecords'."
} else {
Write-Output "Field 'EmployeeID' does not exist in the list 'EmployeeRecords'."
}Here:
$ctx = Get-PnPContext
$ctx.Load($list.Fields)
$ctx.ExecuteQuery()These lines get the current client context using Get-PnPContext, load the fields collection of the list into the context with $ctx.Load($list.Fields), and then execute the query to retrieve the data with $ctx.ExecuteQuery().
Now, when I executed the above PnP PowerShell script, the above error did not come.
This is how to fix the error: “An error occurred while enumerating through a collection: The collection has not been initialized. It has not been requested, or the request has not been executed. It may need to be explicitly requested.“. I hope this will help you save your time and the issue will be fixed. Do let me know in the comments below.
You may also like the following tutorials:
- Check if a File Exists in a SharePoint Document Library Using PnP PowerShell
- Get All List Fields in SharePoint Online using PnP PowerShell
- Download Files from SharePoint Document Library Using PnP PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.