Removing a PowerShell module using its version number

Question:

Is there an easy way to pass version as a parameter to Get-Module?

I have two different versions of Azure PowerShell installed:

And I wish to remove one of them using a command like:

Answer:

Please see: get-help Remove-Module -full

Notice:

FullyQualifiedName parameter accepts a module name that is specified in the format @{ModuleName = “modulename”; ModuleVersion = “version_number”}

Based on that, the following should be what you are after:

Improved Answer (edit)

Did more investigation on this, and there are some quirks when removing using ModuleVersion (which can be a string or a [version]).

If you specify ModuleVersion, Remove-Module will remove all matching modules with that version and greater.

To get an explicit match, you must also pass the guid.

Since that is a lot to type, I suggest adding a function to your profile to make this easier.

and call like:

Testing and analysis

I might as well share how I tested this to arrive at my conclusion. I will leave some details to the reader for self-investigation…

Create two modules, and export a function from each, using different function names to make testing easier.

D:\test\modtest\v1\ModTest.psd1 with version 1.1.0.1
D:\test\modtest\v1\ModTest.psm1

D:\test\modtest\v2\ModTest.psd1 with version 1.2.0.2
D:\test\modtest\v2\ModTest.psm1

Create a function to load the modules, call exported functions, show modules before, remove a module using the parameters specified, show modules after.

call various ways to demonstrate how Remove-Module works…

This is the part, where I leave it to the reader to analyze the output further to verify the conclusions of this article.

That said, this was what I noticed that caused me to investigate further:

Notice that when removing 1.2.0.2, it works as expected. When removing 1.1.0.1, both 1.1.0.1 and 1.2.0.2 are removed!

Source:

Removing a PowerShell module using its version number by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply