An Inconvenient PowerShell Error: “format-default : The collection has not been initialized”

Frustrated Woman — Image by © Mango Productions/Corbis

For many months now, I’ve been getting our favorite kind of error – a sporadic one – when I am running PnP PowerShell which loops through the same code many times. The errors look something like this:

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.
    + CategoryInfo          : NotSpecified: (:) [format-default], CollectionNotInitializedException
    + FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException,Microsoft.PowerShell.Commands.FormatDefaultCommand

In the two cases where it has plagued me the most, I’ve been using a combination of PnP PowerShell, SharePoint Online (SPO) PowerShell, and Sharegate PowerShell to reorganize content in or across several farms or tenants. It’s a potent combination. The three modules together let me do pretty much anything I want, which is great. But in many cases, my PowerShell needs to run over an extended period of time: hours or days.

Sometimes everything runs fine with no errors whatsoever. Other times, I get one of these errors every other time past certain lines. And this usually is exactly the same code, but perhaps running on a different subset of content, or on a different day, or whatever.

It’s been driving me nuts for a long time, and since I only started using PowerShell about 9 months ago, I figured there was just something I didn’t understand. Joy!

Today I think I hit pay dirt. I found an issue in Github (originally posted in 2017!) wherein many people said they have seen a similar error, across many different cmdlets.

Today, my example was a line like this:

Set-PnPListItem -Connection $rootsite -List $matterlist -Identity $thisMatter.Id -Values $matterValues

I’ve debugged that sucker every which way to Sunday. All the values in the variables are right, the connection is right, the credentials are right, and so on and so on. I’ve done try/catches, I’ve logged every bit of info I can think of to the console, you name it. There’s just no darn reason for that line to throw an error.

In that Github issue, a bunch of people have said that simply assigning the result to a variable has solved their problem, like so:

$item = Set-PnPListItem -Connection $rootsite -List $matterlist -Identity $thisMatter.Id -Values $matterValues

Can it be that simple? And why haven’t I found that Github issue before?

Well, I tried it. And the endless errors I was getting today evaporated.

The only plausible theory in that Github issue on this actually makes a bunch of sense. It’s from aronbardsley, and s/he says:

So I understand that in PowerShell – if i call a PnP function (like Add-PnpView) and I don’t store the return value in a variable, PowerShell’s default behaviour is to output the returned value in the console.

Of course, PowerShell – in doing this – attempts to read the returned object’s properties. The object returned is coming from PnP PowerShell – and upon accessing the properties of the object, PnP throws an exception saying it is uninitialised.

It only happens occasionally, making me think it’s some sort of race condition when calling functions inside the PnP framework – like the query is executed, but the function returns before it’s data is made fully available to the return object.

The thing is, the command still executes… but the error stops my entire script. I run long PowerShell scripts to make deployments to SharePoint. I can say without a doubt, this happens at least once every time i run a script – at different points in the script each time.

What that sounds like to me is some sort of incompatibility between the PnP PowerShell module and PowerShell itself. Since it’s sporadic, no one can hand that issue a reproduceable instance, even though it happens with multiple cmdlets.

I’ve learned to use PowerShell, but not to work on the code for the cmdlets. I hope this is something that gets fixed at some point, and maybe aronbardsley‘s idea is what will help that happen.

In the meantime, don’t leave those “bare” cmdlet calls in your PowerShell, and you’ll be more likely to see your script work more reliably. I’m keeping my fingers and toes crossed on this. I’ll report back here if it’s isn’t the miracle I believe it is.

Similar Posts

14 Comments

  1. Hi Marc,

    I noticed the same behavior – and as far as I can tell this is reproducable. I always get this error-message. And I think this is absolutely plausible – there are certain properties in CSOM (which is the underlying “bare metal” of the powershell-cmdlets), which are lazy loading. So these properties have to be explizitly loaded, otherwise you’ll encounter this message.

  2. I’m new to Powershell too and this issue has been driving me crazy. Almost made me want to quit and become a gardener.

    I tried what you suggestion above and it still didn’t work for me when I tried to use the Get-PnPNavigationNode -Location QuickLaunch command.

    Turns out that setting it to a variable is a red herring. I found the below which explains the issue and solution in more detail:

    https://github.com/pnp/PnP-PowerShell/issues/1370#issuecomment-582862614

    Works fine now if I use this:
    Get-PnPNavigationNode -Location QuickLaunch | Select-Object -Property Title, Url, Id

    BTW. It’s great to see you still working on SharePoint and sharing posts Marc. I was a massive fan of your work from the SPServices days. In fact, your name got mentioned in a Team call earlier between some developers who were reminiscing the olden SharePoint days. You created a legacy! haha.

    1. @Adam:

      It’s certainly great to hear I’ve made a difference.

      It sounds like the variable trick won’t solve all things, but it certainly helps with many. I’ve seen this error with cmdlets which are so simple, no embellishment is really possible.

      Your link shows another solution which will definitely come in handy, especially when cmdlets are chained together.

      M.

  3. Thanks, Problem started poping up this morning. Stored the result of an Add-PnpListiTem in a variable solved.

    Explanation seems plausible. Thanks again

  4. Seems like the solution is to not use PnP. Regular old CSOM and powershell allows you to load stuff in context and that seems to work as well. The PnP people really need to fix this, or their libraries are useless.

  5. It’s ugly coding, but I worked for me too. Just putting the lines into a dummy variable saved Christmas for now (deadline jan 1).

    Now I just have to force SharePoint to run a job in between site configuration tasks and I’tll be all right.

  6. Ran into the same issue with the New-PnPWeb command and used your technique to resolve it. Just an FYI, if you run PNP-Powershell from an Azure Automate powershell runbook, you have to use this same technique.

  7. THANK YOU SO VERY MUCH for saving my time (and sanity), works like magic. Used it for cmdlets “New-PnPList” and “Add-PnPFolder”.

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.