Bird
Raised Fist0
PowerShellscripting~5 mins

Module manifest (.psd1) in PowerShell

Choose your learning style10 modes available
Introduction

A module manifest (.psd1) is a file that describes important details about a PowerShell module. It helps PowerShell know what the module contains and how to use it.

When you create a PowerShell module and want to share it with others.
When you want to specify which scripts, functions, or commands are part of your module.
When you want to add version info, author name, or required PowerShell version to your module.
When you want to control what users can see or use inside your module.
When you want to include additional files like help documents or scripts with your module.
Syntax
PowerShell
@{
    ModuleVersion = '1.0'
    GUID = 'unique-guid-here'
    Author = 'Your Name'
    Description = 'What this module does'
    FunctionsToExport = @('Function1', 'Function2')
    CmdletsToExport = @()
    VariablesToExport = @()
    AliasesToExport = @()
    NestedModules = @()
    RequiredModules = @()
    PowerShellVersion = '7.0'
}

The manifest is a hashtable inside a .psd1 file.

Each key describes a part of the module, like version or exported functions.

Examples
This manifest defines a simple module with one function exported.
PowerShell
@{
    ModuleVersion = '1.0'
    GUID = '12345678-1234-1234-1234-123456789abc'
    Author = 'Alice'
    Description = 'Sample module'
    FunctionsToExport = @('Get-Info')
    PowerShellVersion = '5.1'
}
This manifest shows a module that requires another module and exports two functions.
PowerShell
@{
    ModuleVersion = '2.0'
    GUID = '87654321-4321-4321-4321-cba987654321'
    Author = 'Bob'
    Description = 'Advanced tools'
    FunctionsToExport = @('Start-Task', 'Stop-Task')
    RequiredModules = @('PSReadLine')
    PowerShellVersion = '7.2'
}
Sample Program

This script creates a module manifest file, saves it, shows its content, defines a function, imports the module, and calls the function.

PowerShell
# Create a simple module manifest file
$manifest = @{
    ModuleVersion = '1.0'
    GUID = '11111111-2222-3333-4444-555555555555'
    Author = 'Jane Doe'
    Description = 'Demo module manifest'
    FunctionsToExport = @('SayHello')
    PowerShellVersion = '7.0'
}

# Save the manifest to a file
$manifestPath = 'DemoModule.psd1'
$manifest | Out-File -FilePath $manifestPath -Encoding UTF8

# Show the content of the manifest file
Get-Content $manifestPath

# Define the function to export
function SayHello {
    Write-Output 'Hello from the module!'
}

# Import the module using the manifest
Import-Module -Name .\DemoModule

# Call the exported function
SayHello
OutputSuccess
Important Notes

The GUID should be unique for each module. You can generate one using New-Guid.

FunctionsToExport controls which functions are visible when the module is imported.

Always save the manifest file with the .psd1 extension in the module folder.

Summary

A module manifest (.psd1) describes your PowerShell module's details.

It controls what parts of the module are visible and required.

Creating a manifest helps others use your module easily and correctly.

Practice

(1/5)
1. What is the primary purpose of a PowerShell module manifest (.psd1)?
easy
A. To store user credentials for the module
B. To execute the module's main script
C. To describe the module's metadata and control its behavior
D. To compile the module into an executable file

Solution

  1. Step 1: Understand what a module manifest is

    A module manifest is a file that contains metadata about the module, such as version, author, and dependencies.
  2. Step 2: Identify the manifest's role

    It controls what parts of the module are visible and required, helping users understand and use the module properly.
  3. Final Answer:

    To describe the module's metadata and control its behavior -> Option C
  4. Quick Check:

    Manifest = metadata and control [OK]
Hint: Manifest files describe modules, not run code [OK]
Common Mistakes:
  • Thinking the manifest runs the module code
  • Confusing manifest with script files
  • Assuming manifest stores credentials
2. Which of the following is the correct syntax to specify the module version in a .psd1 manifest file?
easy
A. 'ModuleVersion' = '1.0.0'
B. ModuleVersion: 1.0.0
C. ModuleVersion = 1.0.0
D. "ModuleVersion" : "1.0.0"

Solution

  1. Step 1: Recall .psd1 file syntax

    A .psd1 file uses a hashtable syntax with key-value pairs, where keys are strings in quotes and values are assigned with '='.
  2. Step 2: Match correct syntax for version

    The correct syntax is a string key in quotes, an equals sign, and the version string in quotes: 'ModuleVersion' = '1.0.0'
  3. Final Answer:

    'ModuleVersion' = '1.0.0' -> Option A
  4. Quick Check:

    Hashtable key-value with '=' and quotes [OK]
Hint: Use quotes and '=' for keys and values in .psd1 [OK]
Common Mistakes:
  • Using colon instead of equals sign
  • Omitting quotes around keys or values
  • Using JSON style syntax
3. Given this snippet from a module manifest (.psd1):
@{
  RootModule = 'MyModule.psm1'
  ModuleVersion = '2.1.0'
  FunctionsToExport = @('Get-Data', 'Set-Data')
}

What will happen when you import this module?
medium
A. Only the functions 'Get-Data' and 'Set-Data' will be available to the user
B. All functions in MyModule.psm1 will be available regardless of FunctionsToExport
C. The module will fail to import due to syntax error
D. The module version will be ignored during import

Solution

  1. Step 1: Understand FunctionsToExport key

    The FunctionsToExport key lists which functions are visible outside the module when imported.
  2. Step 2: Analyze the effect on import

    Only 'Get-Data' and 'Set-Data' will be exported and accessible to users after import.
  3. Final Answer:

    Only the functions 'Get-Data' and 'Set-Data' will be available to the user -> Option A
  4. Quick Check:

    FunctionsToExport limits visible functions [OK]
Hint: FunctionsToExport controls visible functions [OK]
Common Mistakes:
  • Assuming all functions export by default
  • Thinking syntax error exists here
  • Ignoring FunctionsToExport effect
4. You wrote this in your module manifest (.psd1):
@{
  RootModule = 'MyModule.psm1'
  FunctionsToExport = 'Get-Data', 'Set-Data'
}

When importing the module, you get an error. What is the problem?
medium
A. The manifest file must not contain RootModule key
B. RootModule must be a script file, not a module file
C. FunctionsToExport cannot contain more than one function
D. FunctionsToExport should be an array, not a list of strings without @()

Solution

  1. Step 1: Check FunctionsToExport syntax

    FunctionsToExport expects an array of strings, which requires the @() syntax in PowerShell.
  2. Step 2: Identify the error cause

    Writing 'Get-Data', 'Set-Data' without @() is invalid in a hashtable value and causes an error.
  3. Final Answer:

    FunctionsToExport should be an array, not a list of strings without @() -> Option D
  4. Quick Check:

    Use @() for arrays in manifest [OK]
Hint: Use @() to define arrays in .psd1 manifest [OK]
Common Mistakes:
  • Omitting @() for arrays
  • Misunderstanding RootModule usage
  • Assuming single string can list multiple functions
5. You want your PowerShell module to automatically import a required module and only export one function named 'Invoke-Task'. Which manifest entries should you include?
hard
A. @{ RequiredModules = 'HelperModule'; FunctionsToExport = @('Invoke-Task') }
B. @{ RequiredModules = @('HelperModule'); FunctionsToExport = @('Invoke-Task') }
C. @{ RequiredModules = @('HelperModule'); FunctionsToExport = 'Invoke-Task' }
D. @{ RequiredModules = 'HelperModule'; FunctionsToExport = 'Invoke-Task' }

Solution

  1. Step 1: Define RequiredModules correctly

    RequiredModules must be an array of module names, so use @('HelperModule').
  2. Step 2: Define FunctionsToExport correctly

    FunctionsToExport must be an array of function names, so use @('Invoke-Task').
  3. Final Answer:

    @{ RequiredModules = @('HelperModule'); FunctionsToExport = @('Invoke-Task') } -> Option B
  4. Quick Check:

    Use arrays (@()) for RequiredModules and FunctionsToExport [OK]
Hint: Always use @() arrays for RequiredModules and FunctionsToExport [OK]
Common Mistakes:
  • Using strings instead of arrays for RequiredModules
  • Using strings instead of arrays for FunctionsToExport
  • Mixing array and string syntax
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard