Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 846
S. Mitchell Avatar asked Sep 10 '16 13:09

S. Mitchell


People also ask

How do I force a PowerShell import-Module?

Starting in PowerShell 3.0, installed modules are automatically imported to the session when you use any commands or providers in the module. However, you can still use the Import-Module command to import a module. You can disable automatic module importing using the $PSModuleAutoloadingPreference preference variable.

How do I automatically load PowerShell modules?

Module Auto-Loading Beginning in PowerShell 3.0, PowerShell imports modules automatically the first time that you run any command in an installed module. You can now use the commands in a module without any set-up or profile configuration, so there's no need to manage modules after you install them on your computer.


4 Answers

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
like image 71
DAXaholic Avatar answered Oct 11 '22 12:10

DAXaholic


Try the following:

Import-Module 'E:\xxx.ps1' -Force
like image 30
Khachornchit Songsaen Avatar answered Oct 11 '22 13:10

Khachornchit Songsaen


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.

like image 5
dhbramblett Avatar answered Oct 11 '22 12:10

dhbramblett


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).

like image 3
Matthew Heimlich Avatar answered Oct 11 '22 13:10

Matthew Heimlich