Wracking my brain to get alias to work for my psm1 module

New to powershell, I have spent well over an hour trying to set an alias for my new module… Really not impressed with the lack of instructions on this.

When storing a module.psm1 file in the correct My Documents folder, the function automatically loads and is callable with Get-QuestionAnswer when we start a new powershell session, However my Alias isnt callable and doesnt show in the list of aliases

I have tried setting alias using parameter [Alias('gqa')]

eg

function Get-QuestionAnswer
{
    [CmdletBinding()]
    [Alias('GQA')]
    [OutputType([int])]
    Param
    (
        [Parameter(Mandatory=$false,
         ValueFromPipelineByPropertyName=$true,
        Position=0)]
        [int]$Length
    )
     Write-Output "hello"
}

I have also tried adding

New-Alias gqa Get-QuestionAnswer

And

Set-Alias gqa Get-QuestionAnswer

Also tried adding both together

also tried

Set-Alias gqa Get-QuestionAnswer
Export-ModuleMember -Function Get-QuestionAnswer -Alias gqa
NewAlias gqa Get-QuestionAnswer
Export-ModuleMember -Function Get-QuestionAnswer -Alias gqa

Nothing works, and there is no error message to examine.

Complete waste of human productivity…

Pretty powershell does not export aliases by design. You will need to use a Powershell manifest file (.psd1) to export aliases

New-Alias gqa Get-QuestionAnswer
Export-ModuleMember -Function Get-QuestionAnswer -Alias gqa

This should work … But make sure that after each change you import your module using -force. Without this switch, your module will not get updated with the latest code, it will just use the one already in memory.

Import-module MyCustomModule -force
gqa

I changed my .psm1 extension to .psd1, now neither the function or alias load…

This only works if you manually call Import-module MyCustomModule -force -verbose.
per new powershell session.

If you don’t do this, the Alias will not be set. - IE after closing terminal window, restarting your machine, etc.

Bug?

Is there a way to get this to work automatically… I cant manually call Import-module on all of my modules just to get the aliases to work…

you can also have a try using -scope global when invoking new-alias

Module Manifest files (PSD1) have specific criteria. You can get more on that here:

You may want to have a look at the “AliasesToExport” section for more detail on Aliases.

Not sure how your module is formatted or build, this is a common structured schema. If the module was named Riddler:

Riddler

-Riddler.psd1
-Riddler.psm1
-Private
-Public

-Get-RiddlerQuestionAnswer.ps1

[EDIT] Trying to emulate dir structure is a pain in the forum.

This would be the PSM1:

# Dot source public/private functions
$public  = @(Get-ChildItem -Path (Join-Path -Path $PSScriptRoot -ChildPath 'Public/*.ps1')  -Recurse -ErrorAction Stop)
$private = @(Get-ChildItem -Path (Join-Path -Path $PSScriptRoot -ChildPath 'Private/*.ps1') -Recurse -ErrorAction Stop)
foreach ($import in @($public + $private)) {
    try {
        . $import.FullName
        if ($import.BaseName -like '*Riddler*') {
            New-Alias -Name ($import.BaseName).Replace('Riddler','Rdl') -Value $import.BaseName
        }
    } catch {
        throw "Unable to dot source [$($import.FullName)]"
    }
}

#Manual Alias
New-Alias -Name gqa -Value Get-RiddlerQuestionAnswer

Export-ModuleMember -Function $($Public | Select-Object -ExpandProperty BaseName) -Alias *

The above would make an Alias for each function\cmdlet Get-RiddlerQuestionAnswer would have an Alias of Get-RdlQuestionAnswer. When the module is exported, you also need to export the Aliases with -Alias *.

1 Like

Did you have something like this in mind? :wink:

└───Riddler
    │   Riddler.psd1
    │   Riddler.psm1
    ├───Private
    └───Public
            Get-RiddlerQuestionAnswer.ps1

Yes, spaces…ignored, used greater than (>) and that rendered funky too, periods it considers ellipsis and more than 3 is truncated to 3. Guess I need @Olaf 's guide to forum formatting and etiquette, volume 1?

Actually I created that structure and outputted it with tree … formatted as code and there you go. :wink:

1 Like

That’s too easy. Needs to be a bit more convoluted. :wink:

1 Like