New to PowerShell? Use Classes

Use ClassesAvoid common pitfalls of PowerShell with ClassesChristopher KuechBlockedUnblockFollowFollowingMay 8Before you continue, learn the basic semantics of using and defining Classes: About ClassesPowerShell has powerful but sometimes unintuitive programming features that can cause headaches for new users.

These features make PowerShell the ideal candidate for almost any DevOps task, but often intimidate novice PowerShell devs into choosing substantially more cumbersome languages like C# or Python.

By using Classes, new PowerShell developers can take their first steps into the PowerShell ecosystem without retreating from unwelcome surprisesClasses are simpler than functions?In most languages, functions are simpler than Classes.

Static Classes are basically just ways to organize functions, but PowerShell already supports modules for organizing functions, so why would we want to use static Classes when we could use modules and functions?Equivalent implementations of a trivial function using static method vs a functionSimplified inputsPowerShell functions are extremely powerful.

With a few lines of parameter attributes, you can have a full featured command-line interface for your function.

These interfaces are great for user-facing functions, but this user-facing presentation logic should not be kept with our core implementation logic.

Method inputs are much simpler, with each parameter being mandatory by default, and therefore can keep internal business logic simple.

By isolating business logic, you can implement unit tests around only your core business logic and lean on the declarative parameter validation for code hygiene on your presentational cmdlets.

Simplified outputsOne of the biggest surprises for people learning PowerShell is that return statements aren’t the only statements that output a value from the function — every non-void expression writes a value to the output.

In methods, only return statements output a value, making them easier to test, debug, and comprehend.

Methods also support simple return type declaration in the method headers, ensuring your function always returns the type you expect (or throws a type error).

GraduationSo you’ve met your deadlines using static Classes, now you’re comfortable with basic PowerShell semantics and you’re read to take your scripts up a notch.

Here’s how you can grow next.

Dive into FunctionsWe avoided Functions because their approach to handling parameters and returns can be confusing, but once you learn the nuances of these features, they can dramatically elevate your code.

Learn about Function Parameters so you can quickly and declaratively make powerful command-line interfaces, or make your scripts more stable with parameter validations.

Stop thinking of outputs as singular values and think of them as a stream.

Learn how to redirect the stream and operate upon it in a pipeline.

Take Classes to the next levelClasses aren’t just for beginners.

Learn to leverage advanced features, like implicit and explicit type conversion functions implemented through Classes, so you can amplify your functional code with classes.


. More details

Leave a Reply