Get-Module list module manifest .psd1 file

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of Bojan Zivkovic Bojan Zivkovic 1 year, 3 months ago.

  • Author
    Posts
  • #36938
    Profile photo of Bojan Zivkovic
    Bojan Zivkovic
    Participant

    Hi, having written PowerShell module with bunch of advanced functions next step was to create module manifest.

    $manifestParams = @{
    Author = "Bojan Živković"
    CompanyName = "My Company"
    RootModule = ".\Tools.psm1"
    Path = "Tools.psd1"
    }

    New-ModuleManifest @manifestParams

    I played with FunctionsToExport and loading of .psd1 worked well. However I noticed in Path property of a module there was a path to .psm1 file instead of .psd1 file. For instance path for built-in module Microsoft.PowerShell.Utility points to .psd1 file. This does not affect functionality but still I want to know how to check whether module manifest exists for some module since there is no cmdlet Get-ModuleManifest.

  • #36940
    Profile photo of Lawrence Gulliver
    Lawrence Gulliver
    Participant

    One way would be to check the Version property, when no PSD1 file is present it defaults to 0.0 while a Module Manifest has a default version of 1.0

    Alternatively when there is no manifest the GUID defaults to 00000000-0000-0000-0000-000000000000

    Actually the best check might be the RootModule which is blank when there is no manifest but (AFAIK) should always be present in a manifest (EDIT: as Dave pointed out below modules may be listed under NestedModules so you would need to check that too).

  • #36941
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    You can test for a module manifest by looking for (ModuleName).psd1 in the folder that's found on the ModuleBase property of the object returned by Get-Module:

    function Test-ModuleHasManifest([string] $ModuleName)
    {
        $module = Get-Module $ModuleName -ListAvailable | Select-Object -First 1
        if ($null -eq $module) { return $false }
        
        $manifestPath = Join-Path $module.ModuleBase "$ModuleName.psd1"
    
        return Test-Path -LiteralPath $manifestPath -Leaf
    }
    
  • #36942
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Incidentally, those built-in modules that show up as type "Manifest" have no RootModule. Instead, they load up everything else with NestedModules in the manifest, which slightly changes the output of Get-Module.

  • #36943
    Profile photo of Lawrence Gulliver
    Lawrence Gulliver
    Participant

    Ah, so if checking for RootModule you would also need to check NestedModules. Still probably the most reliable means without manually checking the file exists.

  • #36946
    Profile photo of Bojan Zivkovic
    Bojan Zivkovic
    Participant

    Version property of module is 1.0.

You must be logged in to reply to this topic.