Dan Stef Dan Stef - 3 months ago 18
PowerShell Question

How to remove duplicate function

I'm new at working with Powershell (Version 4.0).

I created different modules:

My-Modules
My-Tools
My-HelpfullCommands


In each module directory I have the .psd1 file where I set the
FunctionsToExport='My-*'
. In the same directory is a .psm1 file with different functions in it.

For Example in the "My-Modules.psm1":

My-GetAvailabilityGroup
My-LastSqlBackup
New-SMOConnection


And in the "My-Tools.psm1":

My-BackupRestoreFromAg
My-GetDbSize
New-SMOConnection


When I check the available commands with
Get-Command -Name My-*
I find the expected 4 commands.

BUT: When I check
Get-Command -Name *SMO*
I find the
New-SMOConnection
two times. Once from the module My-Tools and once from the module My-Modules.

When I try to execute the
New-SMOConnection
I get the error message
New-SMOconnection : The term 'New-SMOconnection' is not recognized as the name of a cmdlet, function...
(you know the ObjectNotFound message ;-) )

So, my question is:

Is it possible to "remove" the
New-SMOConnection
functions, so that they are not visible anymore?

Another question:

Why do the
New-SMOConnection
functions even show up with Get-Command?

Shouldn't the
FunctionsToExport
only export the functions that start with
My-
?

Answer

I removed all my modules from the directory and opened ISE again to verify that the New-SMOConnection command is gone. Created then a new Module (My-HelpfullCommands) with only the New-SMOConnection in it. In all the other modules, I removed the New-SMOConnection function and copied them back into the modules directory.

Now the New-SMOConnection is declared only once and I can use it for all my modules.

Everything seems to work as expected.

Next I plan to add all my littel helper functions into the "My-HelpfullCommands" module.