mituw16
mituw16

Reputation: 5260

Powershell Module name

I am working on a powershell module that has many cmdlets. I've been asked if we can add something to the cmdlets to prevent namespace collisions between modules. One thing that has been proposed if we could add something before the verb. ex. dotnet build . Basically is it possible to put something that would be customname add-newitem? I've looked into qualified module names, but that is a bit clunky.

I've also looked into the manifest file defaultcommandprefix, and that does work, but the prefix is still after the verb.

Upvotes: 3

Views: 1763

Answers (3)

Martin Liversage
Martin Liversage

Reputation: 106926

PowerShell has built-in support for resolving cmdlet naming conflicts by providing a way to "namespace" all cmdlets in a module.

In the .psd1 module manifest you can specify a DefaultCommandPrefix. When importing a module with Import-Module you can also specify -Prefix and this prefix takes precedence over the default prefix in the manifest.

If your module contains the following cmdlets:

  • Add-Bar
  • Get-Bar
  • Open-Bar

and you import them with prefix Foo then the following cmdlets become available:

  • Add-FooBar
  • Get-FooBar
  • Open-FooBar

Upvotes: 0

iRon
iRon

Reputation: 23862

Each PowerShell cmdlet can be address by its friendly name, e.g. Get-Item (or an alias, like gi in this case) or by its full qualified name which is basically: <source>\<name>.

Get-Command Get-Item

CommandType     Name                  Version    Source
-----------     ----                  -------    ------
Cmdlet          Get-Item              7.0.0.0    Microsoft.PowerShell.Management

Meaning you can also address the specific cmdlet like this:

Microsoft.PowerShell.Management\Get-Item -Path .\Test

For custom module the source is the module name, e.g.:

Install-Module -Name JoinModule

Get-Command Join-Object

CommandType     Name                  Version    Source
-----------     ----                  -------    ------
Function        Join-Object           3.7.2      JoinModule

JoinObject\Join-Object ...

This basically means that there shouldn't be any need to manually prefix cmdlets with module (or customer) names. Besides there are clear verb-noun naming recommendations that even define a difference between e.g. New-Item and Add-Item. Nevertheless, if you want to be specific on an identity, the naming convention often used is: <verb>-<identity><noun>, e.g. Get-ADUser or even Get-AzureADUser.

Upvotes: 1

Eldrad95
Eldrad95

Reputation: 65

it's not the same, when you use dotnet build you run dotnet.exe with build argument.

I don't think you can do this in powershell (just take a look at the microsoft powershell command for azure or o365 and you will see very long command name to avoid that).

you still can use a name like :

customname-add-newitem

but the best should be :

add-newCustomNameItem

Upvotes: 1

Related Questions