Understanding Script Modules vs Binary Modules in PowerShell
📖 Scenario: You are learning how to organize PowerShell code into reusable parts. There are two main ways: script modules and binary modules. Script modules are simple text files with PowerShell code. Binary modules are compiled code in DLL files. Understanding the difference helps you choose the right way to share your scripts.
🎯 Goal: You will create a simple script module example, then learn how to import and use it in PowerShell.
📋 What You'll Learn
Create a script module file with a simple function
Create a variable to hold the path to the script module
Import the script module using the path variable
Print the result of calling the function from the script module
💡 Why This Matters
🌍 Real World
PowerShell modules help system administrators and developers organize and reuse code efficiently across scripts and projects.
💼 Career
Knowing how to create and use script and binary modules is essential for automation, configuration management, and extending PowerShell functionality in professional IT roles.
Progress0 / 4 steps
1
Create a script module file with a function
Create a script module file named MyScriptModule.psm1 with a function called Get-Greeting that returns the string 'Hello from script module'. Write the exact function code inside the file.
PowerShell
Hint
Use the function keyword to define Get-Greeting. The function should return the exact string.
2
Create a variable with the script module path
In a new PowerShell script, create a variable called $modulePath and set it to the string './MyScriptModule.psm1' which is the relative path to the script module file.
PowerShell
Hint
Use $modulePath = './MyScriptModule.psm1' to store the path as a string.
3
Import the script module using the path variable
Use the Import-Module command with the variable $modulePath to import the script module into your session.
PowerShell
Hint
Use Import-Module $modulePath to load the module from the path.
4
Call the function and print the output
Call the function Get-Greeting and print its output using Write-Output.
PowerShell
Hint
Use Write-Output (Get-Greeting) to show the greeting message.
Practice
(1/5)
1. Which statement best describes a PowerShell script module?
easy
A. It is a plain text file containing PowerShell functions and scripts.
B. It is a compiled DLL containing .NET cmdlets.
C. It requires Visual Studio to run.
D. It cannot be imported into a PowerShell session.
Solution
Step 1: Understand what a script module is
A script module is a .psm1 file that contains PowerShell code in plain text format.
Step 2: Compare with other module types
Binary modules are compiled DLLs, not plain text files, so they differ from script modules.
Final Answer:
It is a plain text file containing PowerShell functions and scripts. -> Option A
Quick Check:
Script module = plain text PowerShell code [OK]
Hint: Script modules are plain text files, binary are DLLs [OK]
Common Mistakes:
Confusing script modules with binary DLL modules
Thinking script modules require compilation
Assuming script modules cannot be imported
2. Which of the following is the correct way to import a script module named MyModule.psm1 in PowerShell?
easy
A. Import-Module MyModule.psm1
B. Import-Module MyModule.dll
C. Load-Module MyModule.psm1
D. Add-Module MyModule.psm1
Solution
Step 1: Identify the correct cmdlet for importing modules
PowerShell uses Import-Module to load both script and binary modules.
Step 2: Match the file extension with the module type
Script modules use .psm1 extension, so Import-Module MyModule.psm1 is correct syntax.
Final Answer:
Import-Module MyModule.psm1 -> Option A
Quick Check:
Import-Module + .psm1 file = correct [OK]
Hint: Use Import-Module with .psm1 for script modules [OK]
Common Mistakes:
Using Load-Module or Add-Module which don't exist
Trying to import .dll as script module
Omitting Import-Module cmdlet
3. Given the following PowerShell code, what will be the output?