Hi all,
I have followed few guides on how to create a PowerShell module with multiple script files instead of just having one huge psm1 file. Since I placed the module in one of the default locations, I am not sure why the module is not auto-loaded. Please advise!
Here are the steps I followed:
- Place the module in the local user`s Document folder:
C:\Users\Documents\WindowsPowerShell\Modules\myScripts
- Create module file psm1 using the same name as the root folder:
C:\Users\Documents\WindowsPowerShell\Modules\myScripts\myScripts.psm1
- Created a manifest file with unique GUID in the same folder as the module file:
C:\Users\Documents\WindowsPowerShell\Modules\myScripts\myScripts.psd1
- Created a folder with all “Public” functions, i.e. the folder will contain all functions, which users are intended to use:
C:\Users\Documents\WindowsPowerShell\Modules\myScripts\Public
- The Public folder contains the required functions/scripts:
- C:\Users\Documents\WindowsPowerShell\Modules\myScripts\Public\Get-Something.ps1
- C:\Users\Documents\WindowsPowerShell\Modules\myScripts\Public\Install-Something.ps1
- Here is the view of the module file, please note, I have created to be very generic so that I can reuse it if need be:
If (Test-Path $PSScriptRoot\Public){
$Public = @( Get-ChildItem -Path $PSScriptRoot\Public*.ps1 -ErrorAction SilentlyContinue )#Dot source the files
Foreach ($import in $Private) {
Try {
. $import.fullname
}
Catch {
Write-Error -Message “Failed to import function $($import.fullname): $"
}
}
}
If (Test-Path $PSScriptRoot\Private){
$Private = @( Get-ChildItem -Path $PSScriptRoot\Private*.ps1 -ErrorAction SilentlyContinue )
Foreach($import in $Public) {
Try {
. $import.fullname
}
Catch {
Write-Error -Message "Failed to import function $($import.fullname): $”
}
}
}
If (Test-Path $PSScriptRoot\Public){
Export-ModuleMember -Function $Public.Basename
} - Here is the view of the manifest file:
Module manifest for module ‘myScripts’
Generated by:
Generated on: 12/09/2018
@{
Script module or binary module file associated with this manifest.
RootModule = ‘myScripts.psm1’
Version number of this module.
ModuleVersion = ‘1.0’
ID used to uniquely identify this module
GUID = ‘’
Author of this module
Author = ‘’
Company or vendor of this module
CompanyName = ‘’
Copyright statement for this module
Copyright = ‘’
Description of the functionality provided by this module
Description = ’ PowerShell module’
Minimum version of the Windows PowerShell engine required by this module
PowerShellVersion = ‘3.0’
Functions to export from this module
FunctionsToExport = ‘*’
Cmdlets to export from this module
CmdletsToExport = ‘*’
Variables to export from this module
VariablesToExport = ‘*’
Aliases to export from this module
AliasesToExport = ‘*’
}
Thank you!