r/PowerShell • u/CryktonVyr • Feb 27 '26
Question on Best Practices
Hello Veterans of Powershell.
A bit of context. Over the last 2 years, I made a couple of Scripts that originaly I kept in seperate PS1 file and used them when needed. Then I learned how to make terminal menus and functions. Now I have 1 huge PS1 file with 140 functions that enable me to navigate from a Main Menu to sub menus, see results on the terminal window and/or export the results to CSV files or Out-Gridview.
I recently read that this is not aligned with best practices. I should instead have a PS1 file per function and call each file instead.
Why though? I feel like I'm missing some context or good team working habits perhaps?
I'm the only one scripting in an IT team of 3 and my colleague using it just uses the menu options as intended.
EDIT: Since I'm getting the suggestion. I already use a custom module file, a custom $profile and custom $global configuration. It's a "work in progress mess" that became bigger over time.
9
u/evasive_btch Feb 27 '26
Just makes it easier to work with, to debug, to get into when you're not familiar with the codebase.
Maybe not 1 function per file, but yeah, clump similiar functions/fields together.
You could also make .psm1-Files, so you could do "Import-Module MyOwnModule".
6
u/AdeelAutomates Feb 27 '26 edited Feb 27 '26
With a collection like that. It is itching to be a module.
Look it up its very easy to make. Especially for internal use compared to publishing for the world to see
5
u/purplemonkeymad Feb 27 '26
Powershell is fairly flexible, so having it all in one file is not going to give you an issue with it running.
It might be harder to maintain if you have to scroll through a large file for interconnected parts.
Personally. I would put every function in a new file, just so I can keep it more contained in scope. If you do have slower loading, it's not uncommon to have a script that will merge everything into a single psm1 file.
That said I would say the first two things you probably want are:
Coding best practices are as old as code and there are still arguments. There are probably as many standards as there are codebases.
4
u/sirchandwich Feb 27 '26
I guess I disagree with this. Unless by “function” you mean like a “process” or a “string of related events”, then that makes sense.
But the point of a function is to make it accessible in instances where you may need to run it more than once. I probably wouldn’t recommend one giant PS1 file for 140 different things. Maybe instead one main router with many psm1 files instead? But no, one file per function is just going to be a mess to maintain.
3
u/Murhawk013 Feb 27 '26
I make modules that contain many functions that relate to the same thing.
For example:
Microsoft graph module
SQL Server module
2
u/PinchesTheCrab Feb 27 '26
I would say it's bad practice because if you keep this in version control (and you should!), then it's very hard to parse out and manage changes when all commits are against the same file.
I think you should pull the functions out of your menu script and put them into one or more modules.
I would use a build script to generate the psm1 file from the contents of your ps1 files (one per function), and I would also generate a psd1 file that includes the names of all the functions, which can be really easy if the file name matches the function name.
Then it simplifies your menu function dramatically and you can update the functions without updating the menu and vice versa, and because modules have their own metadata about what functions they contain, you could even potentially build out your menus dynamically.
2
u/420nukeIt Feb 27 '26
Nice work. Best practice is whatever works / allowed at your org tbh, and what the purpose of the scripts are.
If you’re using them to automate a task, make a run book that just does it on demand for everyone. If you’re using them for monitoring, stick them on an agent that tells you when it’s found something.
2
u/Kirsh1793 Feb 27 '26
Best practices are just guidelines. In your case, I think there are a few things to consider for your tool.
Personally, I would create a module. You could still use a controller script to start the tool which then loads the module. Ideally the module could also be used independently of the controller script. This way, you could even have multiple ways to use the tool or easily change the UI. Consider making multiple modules to group functions with similar intent. But you will have to figure out how to distribute it.
2
u/g3n3 Feb 28 '26
Things change if you want to deploy and have other users. If it is completely personal then chezmoi is nice tool.
2
u/g3n3 Feb 28 '26
It is ultimately too hard to tell how you are set up to give advice. You’ll need sample code of your structure for more reasonable advice.
2
u/g3n3 Feb 28 '26
Menus and sub menus is anti powershell. The shell already has help and discovery tooling. You have a module and the commands can be discovered. You want a psd1.
2
u/vermyx Feb 27 '26
Reusability. You use a ps1 file per function (or a module with "common functions" because you can call them from other scripts. You can copy the code to other functions but that exponentially increases the maintenance because now you are maintaining the same function in multiple locations.
0
u/[deleted] Feb 27 '26
[deleted] — view deleted comment
3
u/[deleted] Feb 27 '26
[removed] — view removed comment