Skip to content

module and script functions shadowed by user-specified aliases; how to avoid this? #7040

@cspotcode

Description

@cspotcode

When writing a PowerShell module with internal functions, how do I prevent those functions from being overridden by user aliases with the same name?

# my-module.psm1

# An internal, helper function.
# Private to this module.
# Should not be visible to, nor affected by, outside factors.
function e {
    echo 'doing internal stuff'
}

# Exported function
function mymoduleaction {
    e
}
Export-ModuleMember -Function mymoduleaction

If the user has created an e alias, for example in their $profile, my module will break because it will invoke the user-defined alias. This is never desirable.

How do I ensure my module's internal functions are unaffected by aliases?

The user cannot be expected to know and avoid the names of all their module's non-exported functions, nor can a module or script be expected to detect and avoid all user-created aliases. What is the solution to this situation?

Related: https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/11088882-scripts-need-better-isolation-from-the-user-s-glob

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or moreWG-Languageparser, language semantics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions