I created a custom powershell .psm1 module but it won't update after an edit

Viewed 280

I created a custom powershell module in the
C:\Program Files\WindowsPowerShell\Modules\PennoniAppManagement directory. Whenever I make changes to a function in the module, then import the module into a script, the updated code won't take effect. Any solutions?

2 Answers

Make sure you remove the already-loaded version of the module from the session before re-importing it:

Remove-Module PennoniAppManagement -Force
Import-Module PennoniAppManagement
  • Normally, Import-Module-Force - by itself - is enough to force reloading of an updated module into the current session.

    • Import-Module -Force implicitly performs Remove-Module before reloading the module (if the module isn't currently loaded, -Force just loads the module normally).

    • Also note that force-reloading a module is not an option if you're loading it via a using module statement (at least as of PowerShell 7.1.2). Notably the using module method of importing is required if a module exports custom class definitions that the caller should see - see this answer for details.

  • Mathias' two-step approach - Remove-Module -Force, followed by Import-Module - is apparently needed in some cases, and seems to be required in yours.

    • It would be good to understand when the two-step approach is needed. Mathias thinks it is related to cached versions of custom class definitions (used module-internally) lingering instead of getting reloaded and redefined when Import-Module -Force is called. That is, while the module overall may get reloaded, it may be operating on stale classes. At least in the simple scenario below I was not able to reproduce this problem, neither in Windows PowerShell 5.1, nor in PowerShell (Core) 7.2.1, but there may be scenarios where the problem does surface.

    • The Remove-Module documentation describes the -Force parameter solely as relating to the - rarely used - .AccessMode property available on a loaded module's module-information object (you can inspect it with (Get-Module ...).AccessMode). The default value is ReadWrite, which allows unloading (removal) of the module anytime. If the property value is ReadOnly, Remove-Module -Force is needed to unload; if it is Constant, the module cannot be removed from the session at all, once loaded - at least not with Remove-Module.

      • Notably, the implicit unloading that happens with Import-Module -Force is not subject to these restrictions and implicitly unloads a module even if its .AccessMode is Constant (as of PowerShell 7.1.2; I am unclear on whether that is by design).

Test code involving reloading a module with a modified class definition, to see if Import-Module -Force is enough:

# Create a template for the content of a sample script module.
# Note: The doubled { and } are needed for use of the string with
#       with the -f operator later.
$moduleContent = @'
class MyClass {{
  [string] $Foo{0}
}}

function Get-Foo {{
  # Print the property names of custom class [MyClass]
  [MyClass]::new().psobject.Properties.Name
}}
'@

# Create the module with property name .Foo1 in the [MyClass] class.
$moduleContent -f 1  > .\Foo.psm1

# Import the module and call Get-Foo to echo the property name.
Import-Module .\Foo.psm1; Get-Foo

# Now update the module on disk by changing the property name
# to .Foo2
$moduleContent -f 2  > .\Foo.psm1

# Force-import (reload) the module and 
# see if the property name changed.
Import-Module -Force .\Foo.psm1; Get-Foo

# Clean up.
Remove-Item .\Foo.psm1

In both Windows PowerShell (whose latest and last version is v5.1) and PowerShell (Core) 7.2.1 (current as of this writing), the above yields, as expected:

Foo1  # Original import.
Foo2  # After modifying the class and force-reloading
Related