2

This is for readability and safety (clobbering duplication functions) and finding a workaround for no namespace support with PowerShell modules.

I want to be able to do something like this:

Import-Module MyHelpers.psm1 -Functions "FuncOne" -as MyHelpers.Func-One

MyHelpers.Func-One -blah sfsdfsdf

This make it obvious where FuncOne lives. for larger scripts I consider this pretty serious requirement.

It would probably be good enough if I could at least explicitly define which functions I'm importing (without being able to rename them). At least I would see where they are coming from. Is there any support for this? If not then I'll just have to name all functions inside of MyHelpers like MyHelpers.Func-One but then PowerShell will complain the verb is wrong; would that also break other things too?

3
  • 1
    Use the Function parameter to restrict the function (s) you want to import. Use the Prefix parameter to add a prefix string to the functions. See Import- module examples 5 and 6 Commented Mar 1, 2020 at 16:13
  • Thats enough for me, I can accept as answer Commented Mar 1, 2020 at 17:01
  • I have posted it as answer. Thanks. Commented Mar 1, 2020 at 19:21

2 Answers 2

3

Here's my comment as answer:

You can use the Function parameter to restrict the function (s) you want to import.

Next, with the Prefix parameter you can add a prefix string to these imported functions.

See Import- module examples 5 and 6

Sign up to request clarification or add additional context in comments.

1 Comment

example 5: Import-Module PSDiagnostics -Function Disable-PSTrace, Enable-PSTrace
1

Theo's answer is correct, just want to point out also that you can fully qualify commands that you call by prefixing them with the module name already, for example:

Microsoft.PowerShell.Core\Import-Module -Name ActiveDirectory

ActiveDirectory\Get-ADComputer $env:COMPUTERNAME

or to your example:

Import-Module MyHelpers.psm1 -Function "FuncOne" -as MyHelpers.Func-One

MyHelpers\Func-One -blah sfsdfsdf

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.