Travis
Travis

Reputation: 2188

How to qualify a command with a module name containing a dash?

In PowerShell, how do you qualify the name of a command (in an invocation) with the name of a module that contains a dash?

Specifically, I am trying to invoke posh-git\Write-VcsStatus, but the interpreter does not recognize this as a module-qualified command. I have already imported the posh-git module; my $PSModuleAutoLoadingPreference is defaulted to All anyway; and I have verified that my installed version of posh-git contains the command Write-VcsStatus. This issue lies in contrast to other examples that do work in my same environment, e.g. PowerLine\New-PromptText.


For reference, my $PSVersionTable is as follows:

Name                           Value
----                           -----
PSVersion                      6.2.0
PSEdition                      Core
GitCommitId                    6.2.0
OS                             Microsoft Windows 10.0.17763 
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Upvotes: 2

Views: 113

Answers (1)

mklement0
mklement0

Reputation: 437618

The problem is unrelated to the presence of - (HYPHEN-MINUS, U+002D) in the module name, as evidenced by the fact that invoking posh-git\Write-GitStatus works.

Instead, the problem is that the posh-git module is defined in a nonstandard way:

As of module versions 0.7.3, exported function Write-VcsStatus is defined as follows:

function Global:Write-VcsStatus { # ...

That is, the function is placed in the global scope, which means that it technically isn't part of the module at runtime (despite being listed as an exported function and even tab-completion thinking it is a regular part of the module).

Therefore, you can only invoke it without the module qualifier posh-git\, either as Write-VcsStatus or, unambiguously, as global:Write-VcsStatus.

I can't speak to why the function is declared this way - the underlying GitHub repo provides no clue at first glance, but you could consider creating an issue there.

Upvotes: 2

Related Questions