Powershell Coding standards

GrumpyOldCoder 21 Reputation points
2020-12-10T10:48:45.783+00:00

Hello.

I have moved from what I saw Dan refer to as “full-on programming” in https://devblogs.microsoft.com/scripting/weekend-scripter-best-practices-for-powershell-scripting-in-shared-environment/ as I’ve coded in many languages and now moving into scripting in PowerShell.

So there seems to be some behaviors that I’d consider bad and before I go postal on team for such practices what is considered “Good” or at least acceptable!

The things I see that; would have been laughed at and printed for further abuse and have cost a lot of penalty drinks in the bar seem the norm in PowerShell… A couple of the “Bad” behaviors:

  • All the code in a single .ps1 code file, of excessive length – Not splitting code into smaller modules to aid debugging (test something small not the universe). Is one 5000+ line file the accepted practice or is there a preferred way to modularize code? Use Import-Module to pull in code?
  • Not using Functions - Again splitting code into functions to do one thing allows easier testing, maybe for experienced coders from structured languages it makes easier to follow as don’t need to look into a function I know works (as tested on its own) can just skip past that. Then I can pull these out for reuse or to keep code readable… back to the first point…
    I did say a couple of dubious behaviors so leave it there, and hopefully someone with a few years of “full-on programming” and PowerShell experience can point me in the direction of a good guide or reference before I rewrite this code to apply some of the computer science rules and rules past bosses enforced…
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2020-12-10T15:48:18.283+00:00

    I had the same experience, but consider that PowerShell is intended for use by non-programmers (e.g. system administrators) as an automation language. While that was hard to swallow, there's absolutely nothing wrong with using functions and putting those functions into a .ps1 file for incorporation in other scripts. Nor is there anything wrong with creating script modules and having functions managed (incorporated) when the module is imported -- especially if you're going to unit-test the functions with, say, Pester.

    Have a look here: PowerShellPracticeAndStyle and here: powershell-style-guide and here (not free, but worth the money): pesterbook -- you can find free stuff in blogs, but I found it easier to read the book than piece together information.

    I surely wish that there was a site such as CPAN (Comprehensive Pearl Archive Network) that organized modules by function (NET, LDAP, TEXT, JSON, MATH, etc.). MS did that with .Net, why not PowerShell? Just sayin'.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.