serialhobbyist
serialhobbyist

Reputation: 4815

What are .ps1 files for in PowerShell modules?

I'm currently learning about PowerShell modules. If you're using a .psd1 manifest file, you have the option to use .ps1 script files as well as .psm1 manfiest files. Why do you need both?

I created a module with both, with the .psm1 set as RootModule and the .ps1 set in ScriptsToProcess and I've noted some differences, but I'm not sure what they add up to.

Upvotes: 2

Views: 5853

Answers (1)

Christopher Douglas
Christopher Douglas

Reputation: 1550

The section of your manifest in which you place the references to the Ps1 files determines how they are executed.

In your case:

  • The ScriptsToProcess will execute the listed PowerShell scripts in the caller's environment prior to importing the module. This makes me think of them as prep scripts.
  • This is because files listed here are not meant to contain functions; it's meant to be a script. If you want additional functions accessible by your module you have a few options:

    1. List them in NestedModules
    2. Include them in your module
    3. Try listing them in the functions to the export section of the manifest. (I have not tried this method, but it's my understanding that it will work the way you want no matter where the function is located.)

Upvotes: 1

Related Questions