none
Powershell: adding a prefix to each (own made) module? RRS feed

  • Question

  • Hi,

    I'd like to add a prefix to each module I make. This way I can easily name my modules and be certain it doesn't conflict with existing ones + easily find my own modules.

    My modules are loaded by default (which I like and would like to keep), otherwise I could ad a section in my profile, something like

    Get-ChildItem ".\_ImportModules" | Where {$_.Name -like "*.psm1"} | ForEach {import-module -prefix LAB }


    Please advise.
    J.


    Jan Hoedt

    Wednesday, October 7, 2015 9:20 AM

Answers

  • Hi Jan,

    Automatic Prefix will work when you are manually loading\importing the modules or importing PSSessions using the -prefix parameter.

    If you want your modules to be loaded by default and intend it to be shared, then you need to manually hardcode the prefixes  or use DefaultCommandPrefix in the module and not rely on others doing it while importing.

    Anyways, use this to load a non-default module using a prefix in your $PROFILE script.

    This command uses an explicit path to identify the module to import.

    Import-Module -Name c:\ps-test\modules\test -Prefix Jan007
    -Name: should take multiple paths, just point it to the folder name same as the modulename, no need to filter and add only *.psm1

    References:

    Connecting to Exchange Online and Exchange On Premises at the same time, use –Prefix

    What’s in a name?  Using prefixes in PowerShell


    Regards,

    Satyajit

    Please“Vote As Helpful” if you find my contribution useful or “MarkAs Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.



    • Edited by Satyajit321 Wednesday, October 7, 2015 11:20 AM
    • Marked as answer by janhoedt Tuesday, October 13, 2015 9:41 AM
    Wednesday, October 7, 2015 11:19 AM

All replies

  • That is not how a prefix works.  A prefix adds a set of characters to the front of ALL commands loaded.

    I think what yo are trying to ask is, "can I add a prefix to my module name?"  Yes you can.  Just decide on a naming convention and stick to it.

     You should also prefix your CmdLet names in the modules.

    The Import-module "prefix" parameter is designed to allow side-by-side of modules with identical verb-noun names.  It is a "deconflicting" technique.


    \_(ツ)_/

    Wednesday, October 7, 2015 9:29 AM
  • Thanks. I know I can use any name for any module or command, so that's actually not the question. I'm looking for a flexibility so that when I have to change the prefix of comands later, I can easily. Not sure if it is possible, that's why I was exploring the prefix command.

    Changing module names isn't that big issue, I could do that with Powershell :-). I'd specifically like to organize my (customized) commands so I can easily find them and rename them in bulk afterwards
    f.e. get-labCoputerName => get-ContosoComputerName
    deploy-LABSoftware => deploy-ContosoSoftware

    Also, if we start to share commands/modules with colleauges, this can come in handy. F.e. a colleague also has his commands named LAB ..., then I could change my commands to my initials, f.e. deploy-JHSoftware.


    Jan Hoedt

    Wednesday, October 7, 2015 11:14 AM
  • Hi Jan,

    Automatic Prefix will work when you are manually loading\importing the modules or importing PSSessions using the -prefix parameter.

    If you want your modules to be loaded by default and intend it to be shared, then you need to manually hardcode the prefixes  or use DefaultCommandPrefix in the module and not rely on others doing it while importing.

    Anyways, use this to load a non-default module using a prefix in your $PROFILE script.

    This command uses an explicit path to identify the module to import.

    Import-Module -Name c:\ps-test\modules\test -Prefix Jan007
    -Name: should take multiple paths, just point it to the folder name same as the modulename, no need to filter and add only *.psm1

    References:

    Connecting to Exchange Online and Exchange On Premises at the same time, use –Prefix

    What’s in a name?  Using prefixes in PowerShell


    Regards,

    Satyajit

    Please“Vote As Helpful” if you find my contribution useful or “MarkAs Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.



    • Edited by Satyajit321 Wednesday, October 7, 2015 11:20 AM
    • Marked as answer by janhoedt Tuesday, October 13, 2015 9:41 AM
    Wednesday, October 7, 2015 11:19 AM