Skip to content

PowerShell included modules does not have a version directory like Windows PowerShell had, causing assembly conflict on module import #21199

Description

Prerequisites

Steps to reproduce

Issue

I recently experienced a problem with importing a newer version of Microsoft.PowerShell.PSResourceGet (v1.0.2) than what is included with PowerShell 7.4.1 (v1.0.1):

I started researching and noticed that:

  • With Windows PowerShell, included modules are added to a version directory:
    • %ProgramW6432%\WindowsPowerShell\Modules\<modulename>\<version>\<content>
  • PSResourceGet, PowerShellGet and PackageManagement installs modules to:
    • <installpath>\<modulename>\<version>\<content>.
  • PowerShell does not have this version directory for included modules:
    • %ProgramW6432%\PowerShell\7\Modules\<modulename>\<content>

Is this intentional? Can this cause the issue I experience with PSResourceGet?

My understanding of the issue

It seems that when Import-Module searches through $env:PSModulePath for available modules to import, and stumbles across a module that is not contained in a directory consisting of its' version number, it will lazily import assemblies and thus block importing of any other version of the same module.

Ways to mitigate this

Three different ways to get Import-Module -Name 'Microsoft.PowerShell.PSResourceGet' -Version 1.0.2 to work.

  • Delete the included module: %ProgramW6432%\PowerShell\7\Modules\Microsoft.PowerShell.PSResourceGet
  • Move included module to a versioned directory:
    • From: %ProgramW6432%\PowerShell\7\Modules\Microsoft.PowerShell.PSResourceGet\*
    • To: %ProgramW6432%\PowerShell\7\Modules\Microsoft.PowerShell.PSResourceGet\1.0.1\*
  • Reorder $env:PSModulePath to have the path with the newest version of the module first.
    • I do this in my PowerShell profile now.

Expected behavior

PS> Import-Module -Name 'Microsoft.PowerShell.PSResourceGet' -RequiredVersion 1.0.2
PS>

Actual behavior

PS> Import-Module -Name 'Microsoft.PowerShell.PSResourceGet' -RequiredVersion 1.0.2
Import-Module: Assembly with same name is already loaded
PS>

Error details

See: #21201

Environment data

  • Windows 11 23H2.
  • PowerShell 7.4.1 with Microsoft.PowerShell.PSResourceGet v1.0.1 included/embedded.
  • Microsoft.PowerShell.PSResourceGet v1.0.2 installed in some other directory added to PSModulePath.

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs-TriageThe issue is new and needs to be triaged by a work group.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions