📖 Scenario: You are packaging a PowerShell module to share with your team. To do this properly, you need to create a module manifest file (.psd1) that describes your module's metadata and settings.
🎯 Goal: Build a simple PowerShell module manifest file (.psd1) with essential information like module name, version, author, and description.
📋 What You'll Learn
Create a hashtable named ModuleManifest with specific keys and values
Add a ModuleVersion key with a version string
Include author and description information
Output the manifest content as a formatted string
💡 Why This Matters
🌍 Real World
Module manifests are used to describe PowerShell modules so others can understand and use them properly.
💼 Career
Knowing how to create and manage module manifests is essential for PowerShell developers and system administrators who package and share reusable scripts.
Progress0 / 4 steps
1
Create the basic module manifest hashtable
Create a hashtable called ModuleManifest with these exact keys and values: ModuleName set to 'MyModule', Author set to 'Jane Doe', and Description set to 'This is a sample PowerShell module.'
PowerShell
Hint
Use @{} to create a hashtable and assign keys with =.
2
Add the module version to the manifest
Add a key ModuleVersion with the value '1.0.0' to the existing ModuleManifest hashtable.
PowerShell
Hint
Add the new key and value inside the hashtable using the same syntax as before.
3
Convert the manifest hashtable to a formatted string
Use ConvertTo-Json and Out-String to convert the ModuleManifest hashtable into a JSON formatted string and store it in a variable called ManifestString.
PowerShell
Hint
Use the pipeline | to send ModuleManifest to ConvertTo-Json and then to Out-String.
4
Display the module manifest content
Print the content of the ManifestString variable to the console using Write-Output.
PowerShell
Hint
Use Write-Output followed by the variable name to print the string.
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
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 C
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
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 A
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):
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
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 D
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
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').