Skip to content

/errors/microsoft-graph/function-capacity-4096-has-been-exceeded

Import-Module : Function cannot be created because function capacity 4096 has been exceeded for this scope

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated
tool
Microsoft Graph PowerShell
exit code
1
versions
Microsoft.Graph 2.x, Windows PowerShell 5.1
os
windows

Function Get-MgXxx cannot be created because function capacity 4096 has been exceeded for this scope. is a Windows PowerShell 5.1 limit, not a Graph error: a session can hold at most $MaximumFunctionCount functions, 4096 by default. Import-Module Microsoft.Graph loads all the Graph submodules at once and goes past it. Import only the submodules you need, or raise the limit before importing:

powershell
Import-Module Microsoft.Graph.Authentication, Microsoft.Graph.Users, Microsoft.Graph.Groups
powershell
$MaximumFunctionCount = 32768
Import-Module Microsoft.Graph

PowerShell 7 has no such limit (its preference-variable reference no longer lists $MaximumFunctionCount), so the same import works there.

Reproduce the limit

The message comes from PowerShell itself whenever a scope reaches the limit. Defining more than 4096 functions in Windows PowerShell 5.1 shows it without any Graph module; raising $MaximumFunctionCount in the same scope removes it:

powershell
"Default limit: $MaximumFunctionCount"
try {
  1..5000 | ForEach-Object { New-Item -Path "function:\f$_" -Value { 1 } -ErrorAction Stop | Out-Null }
} catch {
  $_.Exception.Message -replace 'Function f\d+', 'Function fN'
  $_.FullyQualifiedErrorId
}
$MaximumFunctionCount = 32768
1..5000 | ForEach-Object { New-Item -Path "function:\g$_" -Value { 1 } -Force | Out-Null }
'After raising the limit: ' + (@(Get-ChildItem function:\ | Where-Object Name -match '^g\d+$').Count) + ' functions created'

Output

Default limit: 4096
Function fN cannot be created because function capacity 4096 has been exceeded for this scope.
FunctionOverflow,Microsoft.PowerShell.Commands.NewItemCommand
After raising the limit: 5000 functions created

The number in Function fN depends on how many functions the session already had, so the block replaces it.

Fix it in scripts

ApproachWhen to use it
Import only the needed submodules (Microsoft.Graph.Users, Microsoft.Graph.Groups, ...)Best option: faster import, fewer functions
$MaximumFunctionCount = 32768 before Import-ModuleScripts that must load the full Microsoft.Graph module in Windows PowerShell 5.1
Run in PowerShell 7 (pwsh)New automation; Graph PowerShell supports both editions

Find which submodule holds a cmdlet before importing it:

powershell
Find-MgGraphCommand -Command Get-MgUserManager | Select-Object Command, Module -First 1
(Get-Command Get-MgUser).Source

Set $MaximumFunctionCount at the top of the script or in the profile, before the import. In Windows PowerShell 5.1 the accepted values are 1024 to 32768.

After the modules load, connect with Connect-MgGraph and the scopes you need; cmdlets called before that fail with Authentication needed. Please call Connect-MgGraph.

Sources