Modules help organize and reuse code in PowerShell. Script modules and binary modules are two ways to package code for easy sharing and use.
Script modules vs binary modules in PowerShell
Script Module: A .psm1 file containing PowerShell functions and scripts.
Binary Module: A compiled .dll file containing cmdlets written in a .NET language.Script modules are easy to create and edit because they are plain text files.
Binary modules require compiling code but can offer faster execution and more complex features.
# Script Module example function Get-Greeting { param([string]$Name) "Hello, $Name!" } Export-ModuleMember -Function Get-Greeting
# Binary Module example # This is a compiled DLL, so no direct code here. # You load it with Import-Module MyBinaryModule.dll
This example shows how to create and use a simple script module that greets a person by name.
# Create a script module file named GreetingModule.psm1 with this content: function Get-Greeting { param([string]$Name) "Hello, $Name!" } Export-ModuleMember -Function Get-Greeting # Then in PowerShell, run: Import-Module ./GreetingModule.psm1 Get-Greeting -Name "Friend"
Script modules are great for quick development and easy sharing.
Binary modules are better when you need performance or want to use .NET features.
You can use both types together in your PowerShell environment.
Script modules are plain text files with PowerShell code.
Binary modules are compiled DLLs with cmdlets written in .NET.
Choose script modules for simplicity and binary modules for performance or advanced features.
Practice
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 AQuick Check:
Script module = plain text PowerShell code [OK]
- Confusing script modules with binary DLL modules
- Thinking script modules require compilation
- Assuming script modules cannot be imported
MyModule.psm1 in PowerShell?Solution
Step 1: Identify the correct cmdlet for importing modules
PowerShell usesImport-Moduleto load both script and binary modules.Step 2: Match the file extension with the module type
Script modules use.psm1extension, soImport-Module MyModule.psm1is correct syntax.Final Answer:
Import-Module MyModule.psm1 -> Option AQuick Check:
Import-Module + .psm1 file = correct [OK]
- Using Load-Module or Add-Module which don't exist
- Trying to import .dll as script module
- Omitting Import-Module cmdlet
Import-Module .\MyBinaryModule.dll
Get-Command -Module MyBinaryModule
Solution
Step 1: Understand Import-Module with a DLL
Importing a DLL file loads a binary module containing compiled cmdlets.Step 2: Get-Command with -Module parameter
Get-Command lists all commands exported by the specified module, here the binary module.Final Answer:
Lists all cmdlets exported by the binary module MyBinaryModule. -> Option CQuick Check:
Import DLL + Get-Command -Module = list cmdlets [OK]
- Thinking binary modules cannot be imported
- Confusing script and binary module outputs
- Assuming Get-Command does not work with modules
Import-Module MyModule.psm1 but get an error. What is the likely cause?Solution
Step 1: Recognize file extensions for module types
Script modules use .psm1, binary modules use .dll files.Step 2: Understand import errors
Trying to import a binary module with a .psm1 extension causes errors because the file type is wrong.Final Answer:
The file extension .psm1 is for script modules, not binary modules. -> Option DQuick Check:
Wrong extension causes import error [OK]
- Assuming binary modules can't be imported
- Using Load-Module instead of Import-Module
- Ignoring file extension importance
Solution
Step 1: Identify module types and their strengths
Script modules are plain text and easy to write but slower and limited in features.Step 2: Match requirements with module type
Binary modules are compiled DLLs, run faster, and can use advanced .NET features.Final Answer:
Binary module, because it is compiled and supports advanced .NET features. -> Option BQuick Check:
Performance + advanced features = binary module [OK]
- Choosing script modules for performance needs
- Thinking script modules load faster
- Believing binary modules cannot be imported
Ready to prove it?
Pass the test and this pattern moves unknown
