Table of Contents
Function Capacity 4096 Has Been Exceeded for This Scope
In some cases, you get the following error when trying to import a PowerShell module.
PS C:\> Connect-ExchangeOnline
...
Function Set-SweepRule cannot be created because function capacity 4096 has been exceeded for this scope.
At C:\Program Files\WindowsPowerShell\Modules\ExchangeOnlineManagement\3.3.0\netFramework\ExchangeOnlineManagement.psm1:766 char:21
+ throw $_.Exception;
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Set-SweepRule:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : FunctionOverflow
Windows PowerShell includes several built-in variables that determine the maximum number of functions, aliases, and variables you can use before reaching a limit. This can be confirmed using the command Get-Variable Max*Count.
Get-Variable Max*Count
Name Value
---- -----
MaximumAliasCount 4096
MaximumDriveCount 4096
MaximumErrorCount 256
MaximumFunctionCount 4096
MaximumHistoryCount 4096
MaximumVariableCount 4096
As indicated, Aliases, Drives, Errors, Functions, History, and Variables have reached their maximum values. Despite years of PowerShell usage, I’ve never surpassed these limits. It seems there’s a first time for everything. Luckily, the solution is straightforward: we simply need to adjust the maximum values to more reasonable levels.
$MaximumFunctionCount = 8192
$MaximumVariableCount = 8192
If you’re running a PowerShell script you can change it as part of the module and make sure to target the Script scope as follows:
if ($PSVersionTable.PSEdition -eq 'Desktop') {
$Script:MaximumFunctionCount = 18000
$Script:MaximumVariableCount = 18000
}
This fix only applies to Windows PowerShell 5 and not PowerShell Core. PowerShell 7 is unaffected by this as those values were sometimes removed in PR – Remove most Maximum* capacity variables.
PS C:\> $PSVersionTable.PSVersion
Major Minor Patch PreReleaseLabel BuildLabel
----- ----- ----- --------------- ----------
7 4 2
PS C:\> Get-Variable Max*Count
Name Value
---- -----
MaximumHistoryCount 4096
Below is an example when we import Microsoft Graph PowerShell modules into a PowerShell session. When run it using PowerShell Core, we don’t need to increase the limitations.
PS C:\> Get-Module | foreach {Get-Command -Module $_.Name} | measure
Count : 23757
Average :
Sum :
Maximum :
Minimum :
StandardDeviation :
Property :
How many functions modules have?
To check how many functions of a PowerShell module, you can use the below basic code snippet:
$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
Name Version FunctionCount
---- ------- -------------
Microsoft.Graph.Beta.Identity.Governance 2.6.1 1223
Microsoft.Graph.Beta.Files 2.6.1 1026
Microsoft.Graph.Files 2.6.1 977
Microsoft.Graph.Beta.DeviceManagement 2.6.1 957
Microsoft.Graph.Beta.Sites 2.6.1 920
Microsoft.Graph.Identity.Governance 2.6.1 875
Microsoft.Graph.Sites 2.6.1 823
The code snippet below will check the number of commands of a PowerShell script.
$array = @()
foreach ($module in Get-Module) {
$a = (Get-Command -Module $($module.Name) | measure).Count
$object = New-Object -TypeName PSObject -Property $([ordered]@{
Name = $module.Name
Verison = $module.Version
Count = $a
})
$array += $object
}
$array | Sort-Object -Property Count -Descending
Name Verison Count
---- ------- -----
Microsoft.Graph.Devices.CorporateManagement 2.19.0 1133
Microsoft.Graph.Files 2.19.0 1076
Microsoft.Graph.Identity.DirectoryManagement 2.19.0 354
Microsoft.Graph.Groups 2.19.0 350
Microsoft.Graph.DeviceManagement 2.19.0 332
Microsoft.Graph.Applications 2.19.0 301
Microsoft.Graph.Education 2.19.0 280
...
Not a reader? Watch this related video tutorial: