PowerShell

Function cannot be created because function capacity 4096 has been exceeded for this scope

I had a long day today when my long-running script (10 hours) gave me weird errors with Microsoft Graph for Teams. Finally, I solved my mistakes and reran the hand to see if the report would be complete this time. Surprisingly, it gave me an error I'd never seen before. “Function cannot be created because function capacity 4096 has been exceeded for this scope“. The error is at least weird because it's shown on a production server where I've just a handful of PowerShell modules installed, and I've never seen it on my development machine where I've over 200 modules.

The error means I've loaded more than 4096 functions within my scope, and the script was ruined after running for 4 hours. In my particular case, the ExchangeOnlineManagement module broke my script. Still, it can happen anytime if too many variables, functions, or aliases are used.

Fixing Function cannot be created because function capacity 4096 has been exceeded

It seems Windows PowerShell has few built-in variables that define how many functions, aliases, and variables you can have before stopping you from using more. You can verify this with handy command Get-Variable Max*Count.

As you can see above, Aliases, Drives, Errors, Functions, History, and Variables all have their maximum values set. After so many years I've spent using PowerShell, I've never exceeded those values. I guess it's the first time for everything. Fortunately, it's an easy fix. We need to change maximum values to something sane.

$MaximumFunctionCount = 8192
$MaximumVariableCount = 8192

After using those two lines of code before running my time-consuming report – my problems were NOT solved! After investigation, I realized that I was connecting to Exchange which triggers imports of commands inside the module scope and not as part of the Global scope. This means I need to change it as part of the module and make sure to target the Script scope.

if ($PSVersionTable.PSEdition -eq 'Desktop') {
    $Script:MaximumFunctionCount = 18000
    $Script:MaximumVariableCount = 18000
}

Remember that this fix only applies to Windows PowerShell and not PowerShell 7. PowerShell 7 is unaffected by this as those values were sometimes removed in 2016 in PR – Remove most Maximum* capacity variables #2363.

How many functions module have?

While my problem was solved, I was curious about what module has the highest number of functions and how it was even possible that I could reach this limit. After a simple query as below – it seems the biggest offenders are the newest Microsoft. Graph PowerShell modules that have even 800 functions in one module. Just using a few of those can quickly get you in trouble. Fortunately, I usually use PowerShell 7 as my daily driver unless there's a specific reason not to. Unfortunately, I have other issues with PS 7 that sometimes force me back to PowerShell 5.1, like incompatibility between modules and DLL conflicts. But that's a story for another time.

$Modules = Get-Module -ListAvailable
$ListModules = foreach ($Module in $Modules) {
    [PScustomObject] @{
        Name          = $Module.Name
        Version       = $Module.Version
        FunctionCount = ($Module.ExportedFunctions).Count
    }
}
$ListModules | Sort-Object -Property FunctionCount -Descending | Format-Table -AutoSize

Przemyslaw Klys

System Architect with over 14 years of experience in the IT field. Skilled, among others, in Active Directory, Microsoft Exchange and Office 365. Profoundly interested in PowerShell. Software geek.

Share
Published by
Przemyslaw Klys

Recent Posts

Active Directory Replication Summary to your Email or Microsoft Teams

Active Directory replication is a critical process that ensures the consistent and up-to-date state of…

1 week ago

Syncing Global Address List (GAL) to personal contacts and between Office 365 tenants with PowerShell

Hey there! Today, I wanted to introduce you to one of the small but excellent…

5 months ago

Active Directory Health Check using Microsoft Entra Connect Health Service

Active Directory (AD) is crucial in managing identities and resources within an organization. Ensuring its…

7 months ago

Seamless HTML Report Creation: Harness the Power of Markdown with PSWriteHTML PowerShell Module

In today's digital age, the ability to create compelling and informative HTML reports and documents…

8 months ago

How to Efficiently Remove Comments from Your PowerShell Script

As part of my daily development, I create lots of code that I subsequently comment…

8 months ago

Unlocking PowerShell Magic: Different Approach to Creating ‘Empty’ PSCustomObjects

Today I saw an article from Christian Ritter, "PowerShell: Creating an "empty" PSCustomObject" on X…

9 months ago