1

I've written some code in powershell modules, and when my .ps1 file Imports them, it works with a fresh window; but if I stop the script, make some changes to the modules, and re-run the script...for some reason it runs just like it did before the changes were made; I can't test the changes to the script until I open a new window, and re-run the script.

I've tried adding things like Remove-Module <path-to-psm1> -Global, but it still behaves the same; in Powershell 5 and Powershell 7; what else does one need to do to obtain a fresh copy of the module?

Truth be told however, I am nesting the imports, but for a good reason, since some of my modules are called as separate jobs, and so they need to be imported within the job; and additionally the *.psm1's are also imported into another psm1 file that kind of maps their functions (function pointers more or less) within a HashMap.

So basically:

   $dasMods = @("$PSScriptRoot\lib\mod1.psm1";
                "$PSScriptRoot\lib\mod2.psm1";
                "$PSScriptRoot\lib\mod3.psm1";
                "$PSScriptRoot\lib\mod4.psm1";
                "$PSScriptRoot\conf\label_function_log_mapper.psm1";)

   $dasMods | % { Remove-Module $_ -Global }
   $dasMods | % { Import-Module $_ -Global }
...

go.ps1


        Remove-Module -Name .\lib\lib1.psm1 -Global
        Import-Module -Name .\lib\lib1.psm1 -Global

        Remove-Module -Name .\lib\lib2.psm1 -Global
        Import-Module -Name .\lib\lib2.psm1 -Global

        Remove-Module -Name .\lib\lib3.psm1 -Global
        Import-Module -Name .\lib\lib3.psm1 -Global
...

\conf\label_function_log_mapper.psm1


        function doSomething($fileObj, $jlist) {

                Import-Module "$((Get-Item -Path "." -Verbose).FullName)\lib\lockAndLog.psm1"

                lockDelogAndWrite "Did Something..." $fileObj

                return @();
        }
        ...

\lib\lib1.psm1 yes it's worse than that, there's a library for locking and writing to the log here that's imported too...


But for whatever reason I have to reopen a new powershell window, every time I make a change to \lib\libX.psm1

Is there a way around this? Where it can just re-run the script and also reload all the modules?

leeand00
  • 25,510
  • 39
  • 140
  • 297
  • 2
    Try adding `-Force` to all calls of `Import-Module` and a `-Verbose` can't hurt – Santiago Squarzon May 25 '22 at 02:51
  • 2
    @SantiagoSquarzon Yes, I believe that was it. Wow that's annoying!!! Thank you. – leeand00 May 25 '22 at 02:57
  • 1
    Np :) If that worked, you can safely remove all calls to `Remove-Module` I believe – Santiago Squarzon May 25 '22 at 02:58
  • 1
    It's done on purpose to prevent issues. So, as Santi pointed out, to forcefully update the module you would use `-Force`. – Abraham Zinala May 25 '22 at 02:59
  • @AbrahamZinala What sort of issue does it prevent? The development of scripts? The development of modular code in powershell? – leeand00 May 25 '22 at 03:06
  • 1
    If PowerShell were to reload all import modules every time, performance would be bad. For development, an alternative to `-Force` is to use VSCode with PowerShell extension. It creates a new, temporary console, every time you launch a script. Works nicely. – zett42 May 25 '22 at 06:53

0 Answers0