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.
Module manifest (.psd1) in 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.
@{
ModuleVersion = '1.0'
GUID = '12345678-1234-1234-1234-123456789abc'
Author = 'Alice'
Description = 'Sample module'
FunctionsToExport = @('Get-Info')
PowerShellVersion = '5.1'
}@{
ModuleVersion = '2.0'
GUID = '87654321-4321-4321-4321-cba987654321'
Author = 'Bob'
Description = 'Advanced tools'
FunctionsToExport = @('Start-Task', 'Stop-Task')
RequiredModules = @('PSReadLine')
PowerShellVersion = '7.2'
}This script creates a module manifest file, saves it, shows its content, defines a function, imports the module, and calls the function.
# 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
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.
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
Solution
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.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.Final Answer:
To describe the module's metadata and control its behavior -> Option CQuick Check:
Manifest = metadata and control [OK]
- Thinking the manifest runs the module code
- Confusing manifest with script files
- Assuming manifest stores credentials
Solution
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 '='.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'Final Answer:
'ModuleVersion' = '1.0.0' -> Option AQuick Check:
Hashtable key-value with '=' and quotes [OK]
- Using colon instead of equals sign
- Omitting quotes around keys or values
- Using JSON style syntax
@{
RootModule = 'MyModule.psm1'
ModuleVersion = '2.1.0'
FunctionsToExport = @('Get-Data', 'Set-Data')
}What will happen when you import this module?
Solution
Step 1: Understand FunctionsToExport key
The FunctionsToExport key lists which functions are visible outside the module when imported.Step 2: Analyze the effect on import
Only 'Get-Data' and 'Set-Data' will be exported and accessible to users after import.Final Answer:
Only the functions 'Get-Data' and 'Set-Data' will be available to the user -> Option AQuick Check:
FunctionsToExport limits visible functions [OK]
- Assuming all functions export by default
- Thinking syntax error exists here
- Ignoring FunctionsToExport effect
@{
RootModule = 'MyModule.psm1'
FunctionsToExport = 'Get-Data', 'Set-Data'
}When importing the module, you get an error. What is the problem?
Solution
Step 1: Check FunctionsToExport syntax
FunctionsToExport expects an array of strings, which requires the @() syntax in PowerShell.Step 2: Identify the error cause
Writing 'Get-Data', 'Set-Data' without @() is invalid in a hashtable value and causes an error.Final Answer:
FunctionsToExport should be an array, not a list of strings without @() -> Option DQuick Check:
Use @() for arrays in manifest [OK]
- Omitting @() for arrays
- Misunderstanding RootModule usage
- Assuming single string can list multiple functions
Solution
Step 1: Define RequiredModules correctly
RequiredModules must be an array of module names, so use @('HelperModule').Step 2: Define FunctionsToExport correctly
FunctionsToExport must be an array of function names, so use @('Invoke-Task').Final Answer:
@{ RequiredModules = @('HelperModule'); FunctionsToExport = @('Invoke-Task') } -> Option BQuick Check:
Use arrays (@()) for RequiredModules and FunctionsToExport [OK]
- Using strings instead of arrays for RequiredModules
- Using strings instead of arrays for FunctionsToExport
- Mixing array and string syntax
Ready to prove it?
Pass the test and this pattern moves unknown
