78

I have created a module 'ActiveDirectory.psm1' which contains a class in powershellv5. I am importing that module in another file called 'test.ps1' and then calling a method from the class.

test.ps1 contains the following:

using module '\\ser01\Shared\Scripts\Windows Powershell\modules\ActiveDirectory\ActiveDirectory.psm1'

Set-StrictMode -version Latest;

$AD = [ActiveDirectory]::New('CS');
$AD.SyncGroupMembership($True);

It all works as expected BUT when I make a change to ActiveDirectory.psm1 & save the changes they aren't reflected immediately. i.e. if ActiveDirectory.psm1 contains:

write-verbose 'do something';

If I change that to

write-verbose 'now the script does something else';

the output remains 'do something'

I'm guessing it has stored the module in memory and doesn't reload it therefore missing the changes I have made. What command do I need to run to load the most recent saved version of the module?

S. Mitchell
  • 783
  • 1
  • 5
  • 6
  • 13
    `Import-Module ActiveDirectory -force` maybe? See [Powershell: Editing an already imported module](https://stackoverflow.com/q/19045315) – wOxxOm Sep 10 '16 at 13:22
  • 1
    I've created this issue to address the problem of the `using module` statement not reloading the module after changes have been made to it. Please go thumbs up it to up-vote it. https://github.com/PowerShell/PowerShell/issues/7654 – deadlydog Aug 28 '18 at 23:56
  • Possible duplicate of [Editing an already imported module](https://stackoverflow.com/questions/19045315/editing-an-already-imported-module) – Micha Wiedenmann Nov 15 '18 at 14:10

3 Answers3

109

As suggested by wOxxOm, you can try pass the -Force flag:

Import-Module ... -Force

Or if that does not work try to explicitly remove it and then reimport with:

Remove-Module
KyleMit
  • 30,350
  • 66
  • 462
  • 664
DAXaholic
  • 33,312
  • 6
  • 76
  • 74
  • 2
    Import-Module -Force does not seem to have any effect on classes though. There's no way of doing "using module MyModule -Force" that I can tell. Like doing Add-Type with a C# class, they appear to persist for the entire session. I'd love to know a way round that – Dave_J Mar 12 '18 at 12:19
  • I've created this issue to address the problem of the `using module` statement not reloading the module after changes have been made to it. Please go thumbs up it to up-vote it. https://github.com/PowerShell/PowerShell/issues/7654 – deadlydog Aug 28 '18 at 23:56
  • Yes!! I was closing my whole powershell session like a dummy – Kellen Stuart Nov 11 '20 at 18:37
5

From what I've gathered. Import-Module does not import classes. You have to use the "using module " and it has to be in the first line of your script. On top of that problem, the classes appear to be "cached" in some esoteric way that precludes any uninstall-module or remove-module options. I've found I basically need to restart my powershell terminal to clear it.

If classes are not involved use import-module OR install-module. In both cases you can do a get-modules -all or get-installedmodule and then remove-module or uninstall-module. You want to make sure you look for all versions and pipe that to remove/uninstall to ensure you wipe everything out.

dhbramblett
  • 51
  • 1
  • 1
  • 1
    Unfortunately I have the same issue. I have a module with a class but it is not reloaded with Remove-Module, Import-Module. I have to restart the PowerShell session to get updates, etc. Probably same issue as mentioned by matthew-heimlich – Maximojo Jul 20 '21 at 01:39
4

For anyone else coming across this issue, see https://github.com/PowerShell/PowerShell/issues/2505

It seems that there is a known long-standing bug regarding importing of modules that are anything above rudimentary level in complexity (for example, I have a module with a single class and class method that fails to update).

Matthew Heimlich
  • 343
  • 2
  • 13