In Powershell, is there a way to ensure the #Requires -Modules always loads the latest changes?

Viewed 531

I have a Powershell (version 5.1) module that I just added a new function to, but it will not appear in the calling Powershell script.

I created a module, let's say myModule.psm1, and manifest myModule.psd1. The manifest has the following settings:

FunctionsToExport = '*'
CmdletsToExport = '*'
VariablesToExport = '*'
AliasesToExport = '*'

myModule.psm1 is in the myModule directory of a path I have in my PSModulePath. I have also explicitly added the fully qualified path of myModule to the PSModulePath.

In my calling Powershell script, I have #Requires -Modules myModule at the top of the file.

If I add a function to myModule, the calling script does not seem to load the changed module.

The only way to load the change, it seems, is to use Import-Module myModule -Force. It was my understanding that #Requires should take care of this.

1 Answers

#Requires -Modules when supplied with only a name, only checks to see that a module is loaded. It will load it if it's not already loaded, but an old version being loaded still satisfies it.

Alternatively, you can supply a hashtable that provides the name and either a minimum or exact version.

By doing this, you could load an updated version of your module as long as you did in fact update the version with your change (which you should be).

#Requires -Modules @{ ModuleName = 'myModule' ; RequiredVersion = '1.2.3' }

Of course that's not very useful when you're testing something out since you'll have to keep changing the version, in 2 places no less.

When I'm developing modules I usually have a debug script that force loads the module and I run it any time I make a change. If your test script is really just for testing, I recommend you use Import-Module -Force instead of #Requires.

Related