Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I turn a collection of script files into a module?

Do you guys know, whether it's possible to convert PowerShell project consisting solely of functions, into module? What I want to achieve is to create distributable module of all my functions so others can use it. But without spending time of converting all functions into cmdlets.

Each of my functions is in separate file. When I then create *.psd1 and I try to include functions via 'FunctionsToExport', it doesn't work. I can't see my functions after module is loaded.

Is it even possible to export function from module when they're NOT (all of them) inside a .psm1 file? I'm still trying to figure out real differences and use of *psd1 and *psm1 files.

like image 991
Matthew Lowe Avatar asked Oct 23 '25 01:10

Matthew Lowe


2 Answers

Yes, you can turn a bunch of .ps1 files into a module. Create a new folder in your module directory $env:USERPROFILE\Documents\WindowsPowerShell\Modules and put all the .ps1 files in that folder. Also create two text files <foldername>.psm1 and <foldername>.psd1 in the folder, so that you have a structure like this:

$env:USERPROFILE
`-Documents
  `-WindowsPowerShell
    `-Modules
      `-MyModule
        +-MyModule.psd1
        +-MyModule.psm1
        +-script1.ps1
        +-script2.ps1
        :
        `-scriptN.ps1

Put the following line in the .psm1 file, so that it "imports" all .ps1 files:

Get-ChildItem -Path "$PSScriptRoot\*.ps1" | % { . $_.FullName }

and specify your metadata in the module manifest (the .psd1 file), e.g.:

@{
  ModuleToProcess   = 'MyModule.psm1'
  ModuleVersion     = '1.0'
  GUID              = '6bc2ac1e-2e88-4bc3-ac84-ecd16739b6aa'
  Author            = 'Matthew Lowe'
  CompanyName       = '...'
  Copyright         = '...'
  Description       = 'Description of your module.'
  PowerShellVersion = '2.0'

  FunctionsToExport = '*'
  CmdletsToExport   = ''
  VariablesToExport = ''
  AliasesToExport   = ''
}

A GUID can be generated for instance via [guid]::NewGuid().

like image 111
Ansgar Wiechers Avatar answered Oct 25 '25 19:10

Ansgar Wiechers


Here's a very simple way of doing it, without including your functions through dot sourcing mode, as it's done in the other answer:

  1. Create a folder C:\MyModules.
  2. Inside this folder, create an empty file named MyModules.PSM1.
  3. Append to MyModules.PSM1 file, all functions (they don't need to be advanced) you want in the module.
  4. YOU ARE DONE.

Now, you have a folder ( C:\MyModules ) that you must install in the target machine.

To install it in the target machine (per user), copy the folder C:\MyModule to the user's default module location (i.e. folder): $home\Documents\WindowsPowerShell\Modules.

Now, this user can type in any PowerShell session the first letter(s) of any function included in your module, that PowerShell's IntelliSense will recognize the function from your module (and uggest the completion substring).

If you don't like the name MyModule, you can change it, as long as you change the folder name as well as the PSM1 file name.

You can also opt to install your module for all users: help about_modules.