# Powershell Module Best Practices

**URL:** <https://forums.powershell.org/t/powershell-module-best-practices/13290>\
**Category:** PowerShell Help\
**Created:** [October 16, 2019, 12:07am UTC](https://forums.powershell.org/t/powershell-module-best-practices/13290 "2019-10-16T00:07:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![apj513](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@apj513](https://forums.powershell.org/u/apj513)\
**Post date:** [October 16, 2019, 12:07am UTC](https://forums.powershell.org/t/powershell-module-best-practices/13290/1 "2019-10-16T00:07:02Z")

</div>

Hi

What are generally regarded the best practices for PowerShell module structure. I have been scouring the internet for answers and cannot find a definitive article.

I currently use the folder structure described here : [https://xainey.github.io/2017/powershell-module-pipeline/#add-private-and-public-folders](https://xainey.github.io/2017/powershell-module-pipeline/#add-private-and-public-folders) . This works well for me. I recently stumbled upon [https://github.com/PoshCode/ModuleBuilder](https://github.com/PoshCode/ModuleBuilder) . I could not find any examples of it’s use, but reading through the doc it seems to elude to the idea of developing a module where each function is a file (which I already do) and then when you release it to “prod” it is compiled as a single psm1 file ? This module talks about best practices but there is no substantiating evidence of what these best practices are, and why they are regarded as best practices. Is there a guide to good powershell module best practices.

Thanks

&nbsp;

---

<div class="post-metadata">

**Author:** ![kpatnayakuni](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kpatnayakuni/32/19_2.png) [@kpatnayakuni](https://forums.powershell.org/u/kpatnayakuni)\
**Post date:** [October 16, 2019, 3:55am UTC](https://forums.powershell.org/t/powershell-module-best-practices/13290/2 "2019-10-16T03:55:07Z")

</div>

Hi there,

It all depends, since PowerShell is a scripting language we can’t define a best way to achieve something, there is always a better way. In many places instead of modules each function is written in .ps1 file and sourcing them as & when it is needed. Even in my opinion instead of loading the entire module once, you can load the functions whatever is required. This is my idea, but there must be another better approach.

Thank you.

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [October 16, 2019, 7:17am UTC](https://forums.powershell.org/t/powershell-module-best-practices/13290/3 "2019-10-16T07:17:32Z")

</div>

Hard to say if there’s a definitive “best practice” across the board, it all depends on your development and release processes and how you _want_ it to work. In general, I’d say keeping public/private/etc folders is a good idea, helps keep things nice and tidy so you can work on things more easily.

Whether you want to compile it to a single PSM1 for release is up to you. Normally, there will not be a noticeable difference until you have at least _several hundred_ commands in your module. However, if you’re signing your script file(s) I’d recommend compiling a single PSM1 and then signing **just** that. If PS has to process a couple dozen signatures, you’re gonna have some VERY slow module loading times. If it’s one signature on a big file, it won’t even be noticed.

Beyond that, I’d highly recommend Kevin Marquette’s article on building modules: [https://powershellexplained.com/2017-05-27-Powershell-module-building-basics/](https://powershellexplained.com/2017-05-27-Powershell-module-building-basics/)

---

<div class="post-metadata">

**Author:** ![masterofthehat](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/masterofthehat/32/5116_2.png) [@masterofthehat](https://forums.powershell.org/u/masterofthehat)\
**Post date:** [October 16, 2019, 10:59am UTC](https://forums.powershell.org/t/powershell-module-best-practices/13290/4 "2019-10-16T10:59:24Z")

</div>

Somewhere back in the beginning of this forum (or maybe even in the previous version), Don Jones started a thread about Powershell best practices, and it may have some community recommendations about modules. I really thought that it lead to an ebook, but I don’t see it now. Might be worth digging up, though…

---

<div class="post-metadata">

**Author:** ![panzerbjrn](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/panzerbjrn/32/36_2.png) [@panzerbjrn](https://forums.powershell.org/u/panzerbjrn)\
**Post date:** [October 16, 2019, 4:31pm UTC](https://forums.powershell.org/t/powershell-module-best-practices/13290/5 "2019-10-16T16:31:11Z")

</div>

While some things are “Best Practices”, such as having proper headers and comment, others are really just personal preference, such as how to capitalise your variables: $VariableCase/$variableCase/$variablecase

&nbsp;

When a colleague and I was tasked with standardising our PowerShell development and coming up with standards we ~~stole~~ came up with a few:

– Start your scripts with a standard set of comments (name, date, author, purpose and keywords) to easily find them later.  
– Add comments as much as possible.  
– Use simple but meaningful variable names. PascalCase is our current best practice. (e.g. $ServiceName or $CounterName, and not $s, $c or $servicename).  
– Place user-defined variables at the top of script. It makes it easier for you or anyone making changes to those script variables.  
– [Don’t use aliases in scripts](http://blogs.technet.com/b/pstips/archive/2015/02/04/don-t-use-aliases-in-your-scripts.aspx). [Ever](http://www.powertheshell.com/bp_script_alias)! Use the full cmdlet name with its named [parameters](https://technet.microsoft.com/en-us/library/hh847824).

And several more…

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:31pm UTC](https://forums.powershell.org/t/powershell-module-best-practices/13290/6 "2024-05-16T20:31:16Z")

</div>


