PnP PowerShell Format-default error

31 Jan

It was a while ago when I saw the 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 came to my attention again when a script was created which created multiple webs using New-PNPWeb, applied a provisioning template using Apply-PNPProvisioningTemplate, added some fields, lists, views and listitems with their corresponding PnP PowerShell cmdlets.
Sometimes the error ‘format-default’ occurs running the script, but not always and not always at the same line of code.

PowerShell’s default behaviour is to output the returned value to the console.
So when running a cmdlet like Add-PnPListItem the returned value is visible in the console.

Probably some PnP cmdlets output an object where one of the properties has not been loaded by CSOM. By assigning the output to a variable the error message is kind of suppressed and the resulting object is stored in the variable.

$fix = Add-PnPView ...

When you output the variable to the console the error message will be shown again.

Summary

When you experience the error ‘format-default…’ provisioning a site with PnP PowerShell the solution is to always store the return value in a variable.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.