Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

Function Cannot be Created Because Function Capacity 4096 Has Been Exceeded for This Scope

May 19, 2024
in Blog, Powershell
0
ADVERTISEMENT

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
...
ADVERTISEMENT

Not a reader? Watch this related video tutorial:

5/5 - (1 vote)
Previous Post

How to Fix Login to Server smtp.office365.com with Username Failed

Next Post

[Winforms] Creating a Responsive Windows Form Using PowerShell With Runspaces

Related Posts

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024
Ftr19

How to Fix Windows Cannot Read the ProductKey From the Unattend Answer File in VirtualBox

July 26, 2024
Ftr25

How to Update Windows Terminal in Windows 10/11

July 26, 2024

How to Disable The Beep Sound in WSL Terminal on Windows

July 26, 2024

Leave a Reply Cancel reply

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

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory