Add-SPOFile fails with format-default : The collection has not been initialized...


Using the PnP PowerShell CmdLets I'm trrying to add a Word document to a library. When I run the following line as part of a larger script:


Add-SPOFile -Path $path/DocumentTemplates/MyTemplate.docx -Folder DocumentTemplates -Web $web -Values @{Title="My Template";FileLeafRef="MyTemplate.docx"}


Then i'm getting the following error:


format-default : 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'm running the PowerShell ISE and when I put a breakpoint on the above line and copy/paste  the above line in the Console section then it runs wihtout failure. So the above variables ($path and $web are still set correctly as my debug session is still active)


If I then continue with F10 the script will fail with the same error. So it looks like there is a differnce between running this as part of a script or running Add-SPOFile from the console.


Any ideas what the difference may be?


@Erwin van Hunen, I noticed that you answered question with the similar error on Github, i haven't found the same issue.

3 Replies
best response confirmed by Pieter Veenstra (MVP)

you're not surpressing error messages in your script? As such it doesn't have to a be a blocking error. The cmdlet just outputs an object where one of the properties has not been loaded by CSOM. The object is still there. The simplest solution is to assign the output of the cmdlet into a variable:


$file = Add-SPOFile etc. etc.


That will 'swallow' the error message and put the resulting object into the $file variable. You will receive the same error when you simply output the results of the $file variable to the output.



Thanks, that fixed the issue. I understand that for PowerShell to present the object to me it isn't able to get to all of the data (not initialized). Still a bit unclear though why it would work when I run it from the Console in PowerShell ISE and not directly in a script.

@Erwin van Hunen I see you fixed a similar issue in the Add-PnPFile cmdlet, would it be possible for you to commit a similar fix for the other SharePoint cmdlets (e.g. Add-PnPListItem and Set-PnPListItem )?