Thursday, 19 October 2017

Troubles with PowerShell Modules

Working with Azure, I regularly have to run PowerShell CmdLets and scripts to get things done.

Unfortunately this means regularly installing and updating Azure PowerShell modules to make use of the latest and greatest features. Sometimes, too I have to downgrade to earlier module versions just to "get stuff done".

The result can be a nasty mess of my powershell environment, leaving behind modules that cannot be removed. For example, I regularly uninstall the AzureRM module with


Uninstall-Module AzureRM


Which completes without error, but a subsequent

Get-Module -ListAvailable

Still reports that the module and sub-scripts are installed. The situation doesn't improve when you try to uninstall the AzureRM module again, this time you receieve an error stating:
No match was found for the specified search criteria and module names "AzureRM"
So, how do you get rid of modules that "seemingly" can't be uninstalled? One solution I dreamt up is as follows:
Get-Module -ListAvailable | where { $_.Name -like "Azure*" } | Select Name | Write-Output -PipelineVariable modname | ForEach { Uninstall-Module $modname.Name }
This utilises the PipelineVariable common parameter to capture the list of "installed" Azure modules and then forcefully uninstall them one-by-one. Be patient, this can take some time to run, but gets the job done.

No comments:

Post a Comment