19

Classes and Enumerations

PowerShell 5 introduced support for creating classes and enumerations within PowerShell directly. Prior to this, you had to import classes from an assembly written in a language such as C# or create them dynamically, which is complex and beyond the scope of this chapter.

Classes and enumerations have only changed a little in PowerShell 6 and 7. There are numerous enhancement issues open in the PowerShell project on GitHub, but these are slow to make their way into PowerShell. Examples include support for writing interfaces and the ability to define getters and setters for properties.

This chapter explores the following topics:

  • Defining an enumeration
  • Creating a class
  • Argument transformation attribute classes
  • Validation ...

Get Mastering PowerShell Scripting - Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.