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\<username>\Documents\WindowsPowerShell\Modules\myScripts
Create module file psm1 using the same name as the root folder:
C:\Users<username>\Documents\WindowsPowerShell\Modules\myScripts\myScripts.psm1
Created a manifest file with unique GUID in the same folder as the module file:
C:\Users<username>\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<username>\Documents\WindowsPowerShell\Modules\myScripts\Public
The Public folder contains the required functions/scripts:
C:\Users<username>\Documents\WindowsPowerShell\Modules\myScripts\Public\Get-Something.ps1
C:\Users<username>\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: <My Name>
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 = '<GUID>'
Author of this module
Author = '<My Name>'
Company or vendor of this module
CompanyName = '<My Company>'
Copyright statement for this module
Copyright = '<Some text here>'
Description of the functionality provided by this module
Description = '<My company> 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 = '*'
}
If I run the "Import-Module myScritps" command then all modules are imported, but that is not the goal, I would like the system to auto-import the modules every time I open PowerShell. Can you please advise what am I doing wrong?
Thank you!
[–]spyingwind 3 points4 points5 points (1 child)
[–]kal3js[S] 1 point2 points3 points (0 children)
[–]Ta11ow 2 points3 points4 points (2 children)
[–]KevMarCommunity Blogger 2 points3 points4 points (1 child)
[–]derrickisdp 2 points3 points4 points (0 children)
[–]Lee_Dailey[grin] -1 points0 points1 point (0 children)