Export-ModuleMember not working with variable

Hello,

i am using Powershell 5.1 on Windows 10.

I have a collection of scripts / functions that I need often. So I want to store them in a separate module “Tools” and then store this psm1 file in a path from the PSMODULEPATH. For example in a file “C:\Users\myuser\Documents\WindowsPowerShell\Modules\Tools\Tools.psm1”.

Since I don’t want to copy all functions into one file, I want to load the actual scripts dynamically.

In the folder “C:\Users\myuser\Documents\WindowsPowerShell\Modules\Tools\Modules\MyFunc.ps1” I put a test function “MyFunc”.

# Test Function

function MyFunc {
[CmdletBinding()]
	write-host "test"
}

I want to import / export this dynamically in Tools.psm1:

# dynamically import functions

# $moduleMember  = @( Get-ChildItem -Path $PSScriptRoot\Modules\*.ps1 -ErrorAction SilentlyContinue )

# Test
$moduleMember = @( Get-ChildItem -Path C:\Users\myuser\Documents\WindowsPowerShell\Modules\Tools\Modules\MyFunc.ps1 )

# $moduleMember.member is now a system.string which contains
#    "C:\Users\myuser\Documents\WindowsPowerShell\Modules\Tools\Modules\MyFunc.ps1"
# $moduleMember.basename ist now a system.string which contains "MyFunc"

# Dot source the files
Foreach($import in $moduleMember)
{
   . $import.fullname -verbose
}

# This is not working
Export-ModuleMember $moduleMember.basename

# Even this is not working
$var = "MyFunc"
Export-ModuleMember $var

# This is working
Export-ModuleMember "MyFunc"

If i start powershell and type

get-module Tools -ListAvailable

i see the exported command “MyFunc” only if i exported it with a literal string

Export-ModuleMember "MyFunc"

even this is not working:

$var = "MyFunc"
Export-ModuleMember $var

what could be the reason for this?

Thanks in advance,
Gregor

Gregor,
Welcome to the forum. :wave:t4:

You don’t have to. You can organise your different functions in individual files if you want.

Why? If the module is installed properly it will be loaded automatically by PowerShell if needed.

Thanks for the quick reply.

But I do not want to have multiple modules, but only one module “Tools” which contains my frequently used functions.

The file “Tools.psm1” must then be in the PSMODULEPATH in a folder with the name “Tools”.

How can I organize the functions in individual files? The file name “Tools.psm1” is predefined, isn’t it?

I did not recommend that.

In Tools.psm1 you could have this code snippet …

Get-ChildItem -Path $psscriptroot -Filter *.ps1 |
    ForEach-Object -Process {
        . $_.FullName
    }

and then you put each individual function in its own *.ps1 file in the same folder. Especially when your module has a lot of functions that’s a lot easier to maintain. :wink:

(notice the dot in front of $_.FullName separated by a space. That’s called dot sourcing and is used to import external scripts into the same scope of the current executed script.

That’s exactly what i tried in my original post:

# Dot source the files
Foreach($import in $moduleMember)
{
   . $import.fullname -verbose
}

I replaced my Tools.psm1 with your variant and places my Test-Function “MyFunc.ps1” in the same Directory.

This ist not working:

Get-ChildItem -Path $psscriptroot -Filter *.ps1 |
    ForEach-Object -Process {
        . $_.FullName
    }

This ist working:

Get-ChildItem -Path $psscriptroot -Filter *.ps1 |
    ForEach-Object -Process {
        . $_.FullName
    }

Export-ModuleMember "MyFunc"

But this is not working, and i don’t understand why:

Get-ChildItem -Path $psscriptroot -Filter *.ps1 |
    ForEach-Object -Process {
        . $_.FullName
    }

$var = "MyFunc"
Export-ModuleMember $var

I do not either. Sorry. :man_shrugging:t4:

I usually go with

Export-ModuleMember -Function * -Alias *

Did you try

Export-ModuleMember -Function $var

?

No, it’s both not working. Possibly these are some security restrictions here at my facility. I will try this on another PC.

Same in my private environment on windows 11 :thinking:

Check out this psm1. Hopefully it will help.