azurepowershellpowershell-corepowershell-5.1

Can't Install `Az` module in PowerShell 7.3.x


My Windows 11 laptop has two versions of PowerShell installed:

Following MS instructions, I want to install the Az module in PowerShell 7.3, so I use this command:

Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force

This gives me the following error:

Install-Package: The following commands are already available on this system:'Login-AzAccount,Logout-AzAccount,Resolve-Error,Send-Feedback'. This module 'Az.Accounts' may override the existing commands. If you still want to install this module 'Az.Accounts', use -AllowClobber parameter

This error is discussed in this SO post. The solution given there is to remove all the pre-existing Az modules from the PowerShell 5.x installation. This doesn't fit my situation, as (1) I don't have any Az modules currently installed in either version of PowerShell and (2) I'm not interested in modifying my PowerShell 5.x installation. Please notice these results:

# From PowerShell 5.x
PS C:\Users\BArias> Get-InstalledModule -Name AzureRM -AllVersions -OutVariable AzVersions

Version    Name                                Repository           Description
-------    ----                                ----------           -----------
5.7.0      AzureRM                             https://www.power... Azure Resource Manager Module

And then...

# From PowerShell 7.3.x
PS C:\Users\BArias> Get-InstalledModule -Name AzureRM -AllVersions -OutVariable AzVersions
Get-Package: No match was found for the specified search criteria and module names 'AzureRM'.

I get the same "No match" result for -Name Az. In other words, there should be no reason that I need to remove either Az or AzureRM from my PowerShell 7.3.x, because such things simply aren't there. Furthermore, I am complying with the MS "coexistence" rules of AzureRM and AZ.

So this brings me back to square one. Why am I getting the This module 'Az.Accounts' may override the existing commands error, when my PowerShell 7.3.x does not have AzureRM installed?

It seems like installing the Az module into PowerShell 7.3.x is a no-brainer, so why does it not "just work?" I'm left with the impression that maybe there is something inherently incorrect with attempting to install the Az module into PowerShell 7? For example, is MS simply expecting that I will use the "Azure Cloud Shell" and I shouldn't be fiddling with my PowerShell 7?


Solution

  • Background information:


    Therefore, to be sure that there are truly no Az-related modules installed, run the following:

    Get-Module -ListAvailable Az* | Select-Object Name, Path
    

    If you find any, uninstall them - possibly from Windows PowerShell - with Uninstall-Module (to uninstall modules in the AllUsers scope, you'll need to run with elevation), and then try installing from PowerShell (Core) again.

    If you do not want to remove installed Azure modules from your Windows PowerShell installation, you can try the following (untested):

    Another, less desirable option would be to uninstall the module from the AllUsers scope in Windows PowerShell and re-install in the CurrentUser scope (of potentially multiple users that need it). Given that PowerShell (Core) sees only the AllUsers-scoped modules installed with Install-Module, moving them to the CurrentUser scope effectively hides them from PowerShell (Core).