Windows 10 PowerShell Modules Question

I’m new to modules and am grouping all of my functions and advanced functions into modules but I don’t want all of them to load until i use them. My Modules folder located here, $home\documents\WindowsPowerShell\Modules has folders and psm1 files like this example: System\System.psm1. modules the main Modules folder are loading automatically into the console. I even removed my profile.ps1 file to test. Can someone educate me on what is happening and what I should do? What do you do?



What do you mean, “the main Modules folder are loading automatically?” How do you know they’re loading automatically?

Thanks for the reply. Ok let me run through this with a better description of what’s happening. I was rushed earlier and trying to quickly post this question while keeping two 2 year olds off of my lap.

So I have my powershell profile setup under: Documents\WindowsPowerShell\Modules\System folder, and a System.psm1 file in the System folder. I have no profile.ps1 file at the moment while testing this. I open the console and it’s a default console load. I type something to run from my system module. example: ‘computername’ | test-online | get-loggedonusers. I hit enter and it runs my function from the module. I thought I needed to import the module first before I could use it. I have nothing loading this module into the console but it’s loaded in. I do a remove-module system -verbose and it tells me it’s been removed. I clear-host and run the command again and it runs my module without errors. I’m not sure why it’s getting loaded when I want a clean console to open by default and then load and unload my modules as I need them. One thing I have done while testing is this. I rename the Modules folder to something different and the console opens clean and my script module doesn’t run the command. So do I need to use a custom path to keep modules from loading into the console if they are in the Modules folder?

Thanks Don, You have helped me before in the past many times so once again I appreciate your help.


So, what’s in \Modules\System\System.psm1 isn’t a “profile.” That’s a module.

Your module is auto-loading. That’s a specific feature of v3 and later. The module doesn’t “load” up front. Rather, PowerShell pre-scans all paths in the PSModulePath environment variable, and makes a note of the commands it finds. That makes the commands available for auto-complete, and it means when you run the command the module is loaded at that time so the command can run.

Try this: Open a new shell. Run Get-Module. Notice your module isn’t loaded. Run your command. Run Get-Module again, and notice your module is loaded.

Pretty sure I described the behavior in “Learn PowerShell in a Month of Lunches,” but now you’ve got me wondering, so I’ll go look :). We’re going to be doing a revision and this would be a good time to add it.

So as long as I have my modules in that Modules folder then they are available to me in a freshly open console but they are not actually loaded into memory until I actually run them. So having your profile load the modules is no longer really needed. If I have modules some place other than the default path that PSModulePath looks at then I do need to load them or use my profile to load them. If I remove-module after using it it is no longer loaded into memory but still available to run again because of the PSModulePath. This clears things up for me a lot. I didn’t know they were being made available to the console automatically. I thought they were being loaded into memory somehow.

This helps me greatly in what I want to do. I don’t want anything to run and load into memory from my profile anymore. I want to have a fresh clean shell every time it’s launched. I want to only use some form of writing to the console for information in my profile and have all of my variables, aliases and functions load with each module.

I used to load all of my functions into memory by dot sourcing them and then I had my profile write-host a menu of functions available. I don’t know what the best practice is for profiles, modules and functions but I figured since I am changing everything over to modules I wanted them to only be in memory when they are used. All of my functions are designed for managing computer labs. Some of the functions are for: copying a file, removing files, setting desktop wallpapers, restarting computers, pinging computers, checking for logged on computers, running remote commands on computer, and more… I can chain these together in the pipeline for example I can ping, check if not logged on, restart computer. it will only restart the computer if it’s pingable and no one is logged on. these type of functions and the pipeline are very handy in a computer lab environment.

Thanks for clearing this up for me. I actually searched for a good while and never found anything that clearly explained why the modules would run when I didn’t load them. Now I know why. I have almost all of your books so I will look into this more as well. Maybe I need to go back to PowerShell Toolmaking and review it.

Thanks, Don