Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Unload Module... completely

I'm working on debugging a Powershell project. I'm using Import-Module to load the PS module from my C# dll and everything works fine. Calling Remove-Module does not fully unload the module though as the DLL is still locked and can not be deleted.

Is there a way to get PSH to fully unload the module and release the DLL so that I can copy over it and reload it again using Import-Module without restarting the PSH console?

Update
So if you load a module into a seperate AppDomain does it still work like a normal module? Can anyone provide an example?

like image 853
Eric Schoonover Avatar asked Aug 26 '09 22:08

Eric Schoonover


People also ask

How do I completely Uninstall a PowerShell module?

To uninstall the Az PowerShell module, you can use the Uninstall-Module cmdlet. However, Uninstall-Module only uninstalls the modules specified for the Name parameter. To remove the Az PowerShell module completely, you must uninstall each module individually.

How do I unload a PowerShell module?

Use the Unload method from AppDomain to unload the application domains. For more information, see Unloading an Application Domain. You could start a new PowerShell host in a new AppDomain, import your module to the host and do the PowerShell job. The module is as normal as it was running in your previous host.

How do I remove a module?

Step 1: First, go to the project section on the left-hand side of the Android studio. Step 2: Right-click on the project whose module you want to delete and go to the option of Load/Unload modules. Click on the module which you want to delete and click on unload button.

Which removes a module that you added to the current session?

The Remove-Module cmdlet removes the members of a module, such as cmdlets and functions, from the current session. If the module includes an assembly ( . dll ), all members that are implemented by the assembly are removed, but the assembly is not unloaded.


2 Answers

There is a workaround. Open up another instance of PowerShell:

PS > powershell PS > [load DLL] PS > [do work] PS > exit 

After the exit, you'll be brought back to the instance of PowerShell from which you made this call (assuming you made the powershell call inside and instance of PowerShell). You can pass in any of the normal arguments to powershell, so you can use -Command or -File. E.g.,

PS > powershell -Command '[load DLL]; [do work]' # Executes a command and exits PS > powershell -Command '.\myscript.ps1 param1 param2' # Executes the script and exits PS > powershell -File .\myscript.ps1 param1 param2 # Executes a script and exits. 

When PowerShell exits, it will release the lock on the DLL, allowing you to continue working.

All of this was done from the PowerShell command line interface. I haven't tested what happens if you throw powershell in the middle of a script or if this works within ISE. (I suspect it works within ISE.) Even if if doesn't work inside a script, this is still useful during development.

Edit:

Did some checking. So this seems to work fine from within scripts and in ISE, but there's a caveat within ISE. From ISE, you can't read any input from the user while you're inside the separate PowerShell process. If you try, the script or commands stop to wait, but no input box is shown like normal, and of course, you can't type directly into the output window in ISE. So if you need to prompt for input in the middle of [do work], prompt before firing up a new instance of PowerShell, and pass it into the work as a parameter. These aren't problems at all if you're using the regular PowerShell command line.

like image 123
jpmc26 Avatar answered Sep 19 '22 22:09

jpmc26


No. As PowerShell uses .NET underneath it has the same requirements. You cannot unload a dll from a .NET AppDomain without unloading the AppDomain itself. As the PowerShell UI lives in the same AppDomain this is not possible.

like image 38
Mischa Avatar answered Sep 17 '22 22:09

Mischa