1

Is it possible to create classes in PowerShell so that it's possible to have all the form objects in one class, button clicks in a second class and the functions in a third?

Just an idea as I've got a script (+GUI) that's just under 900 lines of code but it's getting a little unmanageable, even though I've divided the code into three different blocks separated by comments (The three sections are functions, forms and click_events), it still means I've got to scroll from the top of the script to the bottom if I want to add a function to a click_event.

2 Answers 2

3

If you are looking for the concept of libary.

You can dot source .ps1 files as shown here. You can create a module .psm1 file (not mandatory but better approch) like shown here.

Dot sourcing is the old fashion coming with PowerShell 1.0. Powershell 2.0 bring modules which are more manageable and really bring the concept of library (script, binary).

Now just think about reuse your libraries between different scripts.

Sign up to request clarification or add additional context in comments.

3 Comments

I agree. Though I use dot sourcing in my psm1 files to load up my supporting ps1 files. I prefer to break out all my functions into separate files. I find it easier to manage, especially when working with other scripters on the same set of scripts.
Thanks guys, I've put the functions and click_events into two seperate .psm1 files and made a module. Is it possible to import two .psm1 files in one module? Thats what I'm trying to do but for some reason I'm being told I cant find the specified module 'test_modules'. Does each .psm1 file have to be in it's own sub-folder?
Yes each module have to be in hown own directory if you want to load it separatly.
1

I face this problem by breaking my script into several scripts, depending on the function, and then using the Import-Module cmdlet. I even have a couple modules that import other modules of lower level functions.

Comments

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.