powershellpowershell-5.0powershell-module

How do I force powershell to reload a custom module?


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?


Solution

  • 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