Improving Load Time for a Custom PowerShell Module

0
6
Asked By TechieTurtle42 On

Hey everyone! I've been developing a PowerShell module named ContosoUtils to help my team with some tasks. The goal was to make it user-friendly for team members who aren't very tech-savvy. I've structured it with public and private functions, where private functions handle the back-end processes, and public ones are accessible through a simple interactive menu.

The module works by loading all relevant scripts when a user starts PowerShell, and I've built it so the users can run a command `menu` to access the functions easily. However, as I've added more scripts, the module's load time has notably increased. Currently, there's about a 30-second delay when starting up, likely due to slow dependencies like the Microsoft Graph modules.

I'm thinking of restructuring the module to help reduce load times. One idea is to implement nested modules, like ContosoUtils.Common and ContosoUtils.Interactive. But I'm unsure if this will help speed up the loading process. What are your thoughts? If I set it up with nested modules, would it still face similar performance issues?

2 Answers

Answered By CodeNinja81 On

The loading of the MS Graph modules typically occurs only when they're actually needed. If you're using `#requires ...`, that may trigger them to load up front when the module initializes. Instead, try using `Import-Module` within the function where you need it.

Also, consider a more efficient loading method for your scripts in the PSM1 file—this can really speed things up! For reference, take a look at how I handle it in a similar project.

ChillDev123 -

Just got back from work! I haven't had time to dig into your file yet, but I did set up `#requires` for the Graph beta modules my script needs. Fingers crossed it works smoothly!

NerdHerder99 -

I peeked at your approach—it's almost identical to how I set up mine, so I think you're on the right track!

Answered By HelperBee88 On

I took a look at what you shared. Your current PSM1 should ideally contain all the module content to streamline performance, so consider ditching `Export-ModuleMember` completely. Here's a rough idea of how your build script could look:

```
ContosoUtils/
--ContosoUtils.psd1
--ContosoUtils.psm1
--build.ps1
```

With `build.ps1`, you compile all your scripts into the single PSM1, which can reduce load times and seamless user experience, especially if they aren't required to call `Import-Module`.

This structure helped me significantly, and I think it could do the same for you!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.