2

I'm going to work on a large power script project. I want to create an architecture for my files and make it more maintainable.

I want to put each function in a separate ps1 file, so I can manage them separately.

For example, consider I want to have 2 ps1 files call it: FunctionA, FunctionB.

Then I want to have a MyFunctions.psm1 which is an aggregation of those 2 functions.

I don't want to write the functions directly in the psm1 file. I want write functions separately in the files, then aggregate them in the module.

The aggregation operation is something like building a project.

My utopia is a Visual Studio Project which I can add ps1 files in it, then, when I build the project a psm1 file would be generated.

I found a CodePlex project (PowerGUI) which is a Visual Studio Extension that adds a PowerShell Project Template.

But, unfortunately, it doesn't seem to have this psm1 auto generating.

Question 1: Is there any way to add this feature to the project, For example, adding something to Build Events.

Question 2: Is there another way of creating a psm1 file from ps1 files. Something like a Aggregate-Functions cmdlet or function!?

4
  • Don't you think that having one file per function is a logistical nightmare, unless your functions are extremely complex and long (which then become a maintenance nightmare ...)? Commented Sep 18, 2014 at 6:56
  • @DavidBrabant Having them in a Visual Studio Project makes it sensible. Each file can be checked-in and checked-out. So multiple developers can work concurrently. So why would it be a nightmare!? Maybe aggregating Modules into a final Module would be a better question? Commented Sep 18, 2014 at 7:07
  • @DavidBrabant - which would you rather have, thousands of lines of code to sift through, or separate files that contain exactly and only what they express to contain? I started out with the former, it was an absolute mess. I now use the former and it is incredibly easy to work with. I suppose it's subjective to a point, and dependent upon the tooling and experience of the end user? Commented Sep 18, 2014 at 11:23
  • @CookieMonster: "or separate files that contain exactly and only what they express to contain". Yes, that's my point: exactly and only what they express to contain. Is one function that granularity? I serioulsy doubt it. Commented Sep 18, 2014 at 12:39

1 Answer 1

2

This is very easy to do with dot-sourcing in psm1. E.g. in the module directory we have:

func1.ps1

function func1 {
    'Hello from func1'
}

func2.ps1

function func2 {
    'Hello from func2'
}

test.psm1

. $PSScriptRoot\func1.ps1
. $PSScriptRoot\func2.ps1

This is it, the module is ready. Now this code imports the module and tests func1 and func2:

Import-Module .\test.psm1
func1
func2

Output:

Hello from func1
Hello from func2
Sign up to request clarification or add additional context in comments.

1 Comment

This is good advice. You can also avoid hard coding the file names and use PowerShell to obtain them (e.g. Get-ChildItem, loop, try to load the file, catch errors). Maybe have a separate spot for private functions to make it easy to differentiate if you use Export-ModuleMember. Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.