# Slow module load

**URL:** <https://forums.powershell.org/t/slow-module-load/24876>\
**Category:** PowerShell Help\
**Created:** [October 15, 2024, 9:55pm UTC](https://forums.powershell.org/t/slow-module-load/24876 "2024-10-15T21:55:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeremyhagan](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jeremyhagan/32/5687_2.png) [@jeremyhagan](https://forums.powershell.org/u/jeremyhagan)\
**Post date:** [October 15, 2024, 9:55pm UTC](https://forums.powershell.org/t/slow-module-load/24876/1 "2024-10-15T21:55:56Z")

</div>

Hi,

I’ve written several custom PowerShell modules recently which provide wrappers around some az and mggraph commands. For some reason these modules are very slow to import. Like 50 seconds or so. The modules themselves only have 1 exported funciton and maybe 10-20 private functions all in one big file.

When I run import-moodule MYModule -verbose, all of the slowness appears to be before it exports the functions.

I tried removing the RequiredModules from the manifest, thinking this was slowing it, but it didn’t make a difference.

Is there a way of troubleshooting this slowness?

Here is a copy of the manifest and root module file:  
Manifest

> @{  
> # Script module or binary module file associated with this manifest.  
> RootModule = ‘ManageAzRbac.psm1’
> 
> ```
> # Modules that must be imported into the global environment prior to importing this module
> RequiredModules = @(
> 'Az.Accounts',
> 'Az.Resources',
> 'Az.ResourceGraph'
> )
> 
> # Version number of this module.
> ModuleVersion = '1.2.0'
> 
> # ID used to uniquely identify this module
> GUID = '789287d1-02ff-4886-b2bd-c8c5171d4a8e'
> 
> # Author of this module
> Author = 'Jeremy Hagan'
> 
> # Company or vendor of this module
> CompanyName = 'Company Dot Com'
> 
> # Description of the functionality provided by this module
> Description = 'Module to create and manage Access Group and Role Assignments in Azure'
> 
> # Functions to export from this module
> FunctionsToExport = @('Set-AzRbac')
> 
> # Cmdlets to export from this module
> CmdletsToExport = @()
> 
> # Variables to export from this module
> VariablesToExport = @()
> 
> # Aliases to export from this module
> AliasesToExport = @()
> 
> # Private data to pass to the module specified in RootModule/ModuleToProcess
> PrivateData = @{
> 
> }
> 
> ```
> 
> }

Root Module

> # Dot source all public functions
> 
> Get-ChildItem -Path $PSScriptRoot\Public\*.ps1 | ForEach-Object { . $\_.FullName }
> 
> # Dot source all private functions
> 
> Get-ChildItem -Path $PSScriptRoot\Private\*.ps1 | ForEach-Object { . $\_.FullName }

---

<div class="post-metadata">

**Author:** ![grey0ut](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/grey0ut/32/5310_2.png) [@grey0ut](https://forums.powershell.org/u/grey0ut)\
**Post date:** [October 16, 2024, 2:52pm UTC](https://forums.powershell.org/t/slow-module-load/24876/2 "2024-10-16T14:52:11Z")

</div>

as a test, get a specific time for the current state

```Powershell
Measure-Command { Import-Module ManageAzRbac }

```

And then make a new iteration of the module where all of your public and private functions live in one monolithic .psm1 file (you could use a loop with Out-File -Append for this). Then measure importing **that** version of the module where all the functions are statically within the psm1 and report back.

If the speeds are significantly faster, then we can blame the dot sourcing. If they are not, there must be something else going on.

---

<div class="post-metadata">

**Author:** ![jeremyhagan](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jeremyhagan/32/5687_2.png) [@jeremyhagan](https://forums.powershell.org/u/jeremyhagan)\
**Post date:** [October 20, 2024, 9:49pm UTC](https://forums.powershell.org/t/slow-module-load/24876/3 "2024-10-20T21:49:25Z")

</div>

Hi,

Moving all the functions into the root module file did nothing to change the slow load time.

---

<div class="post-metadata">

**Author:** ![grey0ut](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/grey0ut/32/5310_2.png) [@grey0ut](https://forums.powershell.org/u/grey0ut)\
**Post date:** [October 21, 2024, 4:06pm UTC](https://forums.powershell.org/t/slow-module-load/24876/4 "2024-10-21T16:06:23Z")

</div>

I just tried manually importing the 3 required modules you specify in your manifest and measuring their time, then I re-read your note and saw that you already tried removing them.  
If you’re only exporting one public function, then I think all that remains is those private functions you mentioned.  
You might try manually looping through all of those and measuring their import time to see if anything stands out:

```Powershell
Get-ChildItem -Path \ModuleFolderPath\Private*.ps1 | Foreach-Object {
    $Time = Measure-Command {. $_.FullName}
    [PSCustomObject]@{
        FileName = $_.Name
        ImportTime = $($Time.TotalSeconds)
    }
}

```

---

<div class="post-metadata">

**Author:** ![jeremyhagan](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jeremyhagan/32/5687_2.png) [@jeremyhagan](https://forums.powershell.org/u/jeremyhagan)\
**Post date:** [October 22, 2024, 12:44am UTC](https://forums.powershell.org/t/slow-module-load/24876/5 "2024-10-22T00:44:51Z")

</div>

I think I have it. It IS the required modules which are slowing things up. The reason I still had the slowness when I removed the RequiredModules statement from the manifest was because I also had the same modules in the #requires statement in the script files.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [November 21, 2024, 12:45am UTC](https://forums.powershell.org/t/slow-module-load/24876/6 "2024-11-21T00:45:21Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
