Skip to content

Make PowerShell Classes Exportable Module Members #6293

Open

Description

@ephos

Steps to reproduce

Currently when classes are defined within a module it seems the best way to access them us with the following.

using module ModuleName
[Foo]::new()
Get-Module ModuleName

Expected behavior

Like other module members (functions, cmdlets, variables, aliases, etc) it would be helpful to be able to declare classes and export them as module members. Module manifests could have a section for this.

ClassesToExport = @()

Using them would feel more in line with how the rest of the language works. With the current implementation I find myself creating C# classes and importing the .cs file with Add-Type.

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.0.1
PSEdition                      Core
GitCommitId                    v6.0.1
OS                             Microsoft Windows 10.0.15063
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      5.1.15063.909
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.15063.909
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Activity

  1. iSazonov commented on Mar 2, 2018

    @iSazonov
    Collaborator

    Dup #4112

  2. added
    WG-Language
    on Mar 2, 2018
  3. rockarruda commented on Mar 2, 2018

    @rockarruda

    Ilya (@iSazonov) This reads more of a feature request to me rather than a bug report.

  4. added
    Issue-Enhancement
    on Mar 2, 2018
  5. iSazonov commented on Mar 2, 2018

    @iSazonov
    Collaborator

    It related with how we plan to design class visibility (public/private).

  6. BrucePay commented on Mar 13, 2018

    @BrucePay
    Collaborator

    In PowerShell today, all public types are global. For example, when you import a binary module all of the public types defined in that assembly are globally visible in their declared namespaces. PowerShell classes work the same way. All types (classes) defined in a module are global, namespace qualified by the module name. (Side note: using Add-Type without namespace qualification is bad idea when creating reusable code as the types will be created in the top level namespace so your [foo] and my [foo] will collide.)

    There is a plan to support type name aliases with the using statement to simplify some scenarios. That said, making PowerShell types work differently from the rest of .NET seems undesirable. (I supposed Export-Type could just export type name aliases into the context where the module is being imported.)

    WRT the private type comment, remember how the debugger works - it's just PowerShell. If a type is not accessible from PowerShell, it's not accessible from the debugger either (which is why we have hidden rather than private members).

  7. adrian-andersson commented on May 10, 2019

    @adrian-andersson

    Bumping this issue
    Can we also have the same for ENUMS, and REQUIREDRUNTIMES

  8. User1785604260 commented on Mar 26, 2023

    @User1785604260

    I just stumbled upon a really easy way to export PowerShell classes from modules:

    # Export module classes
    [psobject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::Add("PSUtils", [PSUtils])

    I add that at the bottom of my module where I have a PSUtils class defined, and now everything works just the way I've always wanted. Import-Module and #Requires -Modules work as-is, and my code that requires the class available at parse time also works. No more slow and limited "using module" needed!

    Obviously this is still just a workaround to missing functionality, but it seems to do everything I need in the meantime.

  9. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  10. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  11. added
    Resolution-No Activity
    on Nov 16, 2023
  12. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  13. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

  14. added
    KeepOpen
    and removed
    Resolution-No Activity
    on Nov 5, 2025
  15. gabriel-vanca commented on Jul 18, 2026

    @gabriel-vanca

    Any progress on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-EnhancementKeepOpenWG-Language

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions