Module Load and Unload Event
See original GitHub issueRequest
The PowerShell engine does not expose events for module authors to subscribe when a module load/unload event occurs. This causes module authors to implement workarounds to identify when a module is loaded/unloaded.
Add module load/unload events.
Background
I have a module that uses other modules to ship “providers” like the PSProvider system. PowerShell is able to inspect modules during load to identify if a module contains a PSProvider and add it to the internal cache of providers. PowerShell is also able to remove the PSProvider from the cache when the module is unloaded. Modules that leverage a provider model outside of the engine have to use workarounds to discover providers inside other modules.
PackageManagement does not import providers when a module is loaded but instead requires the user to call Import-PackageProvider to load the provider. If the module containing the provider is removed the provider is not removed and there is no command to remove it.
SHiPS requires the provider to use a specific naming convention for the SHiPS provider to load the script module provider.
Alternatives
PSModuleInfo.OnRemove
This can be used to inject logic into another module unload action but there is no protection against this logic from being wiped out from the module or other user.
IModuleAssemblyInitializer/IModuleAssemblyCleanup or Script Module
These run in the target module but have to implemented by the other module author.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:6
- Comments:13 (4 by maintainers)

Top Related StackOverflow Question
Yes I’ve also seen good reasons for wanting this, both in my own work and when others have asked. /cc @TylerLeonhardt.
I’m surprised we don’t already have an issue for this btw
@iSazonov This issue is all about subscribing to module load/unload events in order to perform an action. I may not have been as clear as I wanted but “I have the cmdlets update a cache every time a cmdlet or completer is called.” is a workaround to not being able to subscribe to module load/unload events. I have logic in the cmdlet/completer to check which modules have been loaded/unload since the the last time the cache was updated. This wouldn’t be necessary if I was able to subscribe to module load/unload events directly.
I’ll update the description to have a better explanation and request to have module load/unload events.