Scripting
Scripts vs. Modules
An executable text file
Can be a simple script or a collection of them
Contains cmdlets & functions
Contains scripts, cmdlets, & functions
Execute directly (.\script.ps1
)
Is imported (Import-Module .\module.ps1
)
File Extensions
ps1
The *.ps1
file extension represents executable PowerShell scripts.
psm1
The *.psm1
file extension represents a PowerShell module file. It defines what the module is and what is contained within it.
psd1
The *.psd1
is a PowerShell data file detailing the contents of a PowerShell module in a table of key/value pairs (manifest).
Creating a Module
Components
A module is made up of 4
essential components:
A directory containing all the required files and content, saved somewhere within
$env:PSModulePath
.A
manifest
file listing all files and pertinent information about the module and its function. This could include associated scripts, dependencies, the author, example usage, etc.Some code file - usually either a PowerShell script (
.ps1
) or a (.psm1
) module file that contains our script functions and other information.Other resources the module needs, such as help files, scripts, and other supporting documents.
Manifest
A module manifest is a simple .psd1
file that contains a hash table. The keys and values in the hash table perform the following functions:
Describe the contents and attributes of the module.
Define the prerequisites.
Determine how the components are processed.
If you add a manifest file to the module folder, you can reference multiple files as a single unit by referencing the manifest. The manifest describes the following information:
Metadata about the module.
Prerequisites needed to import the module, such as the Windows PowerShell version, the common language runtime (CLR) version, and the required modules.
Processing directives, such as the scripts, formats, and types to process.
Restrictions on the module members to export, such as the aliases, functions, variables, and cmdlets to export.
All the lines in the manifest files are optional except for the ModuleVersion
line.
Script
Protecting Functions
If we don't want our functions to be accessed, exported or utilized by other scripts or processes within PS.
Module
Last updated