Import-Module and the “-Prefix” parameter

Lesson learned from the presentation “How to Position Yourself for the Future” with Jeffrey Snover and Jason Helmick (from the Q&A session at 1:01:17): you can add a prefix to the commands of a module when you import it!

In the Q&A session a guy from audience mentions that a PowerShell module (presumably PowerCLI from VMWware) breaks other PowerShell commands as there are Cmdlets/functions named like core or other modules Cmdlets.

So if you import the PowerCLI module it will provide you with the command Get-VM, which is a good idea if you want to manage virtual machines on vSphere, but nowadays you might also have a Hyper-V server you want to manage from that very same computer (maybe using RSAT) which also has the Cmdlet Get-VM. So now it is no longer possible to manage virtual machines on the Hyper-V servers (at least to run the Cmldet Get-VM against Hyper-V) unless you remove the PowerCLI module from that PowerShell instance (or specify explicitly that the command should be the one from the Hyper-V module).

It is not very likely that VMWare will change their commands anytime soon to not overwrite/conflict with other Cmdlets/functions. So what could you possible do manage both hypervisors within one script?

You can import the module which breaks your code with the parameter -Prefix.

Let’s see how this works. For demo purpose I will use the BitsTransfer module.

So importing the module “as usual” and getting the available commands gives you just what you expect.

But importing it with a prefix will add this very prefix to every command (after the verb-) of that module.

Very cool feature I did not yet know about.