Chapter 19. Classes in PowerShell

 

This chapter covers

  • The basic ideas underlying classes in PowerShell
  • PowerShell class and enumeration creation
  • Detailed discussion of properties and methods in PowerShell classes
  • Method overloading and inheritance
  • Class initialization and construction
  • DSC resources based on PowerShell classes

Oh brave new world that has such people in it!

Miranda in William Shakespeare’s The Tempest

PowerShell has always been a .NET language in that it worked with and consumed the types in the .NET framework, but it was always a kind of second-class citizen compared to other .NET languages because you couldn’t create new types directly in PowerShell. This has been fixed in PowerShell v5, which now supports the ability to define new classes as well as extend existing .NET classes.

Note

The class keyword was reserved in the earliest versions of PowerShell with the intent that the team would eventually add this capability to PowerShell. It only took a little under 10 years to do it because the PowerShell team didn’t want to rush into something as important as this.

In this chapter, we’re going to look at what defining classes allows you to do as a PowerShell scripter/programmer. Also, one of the primary drivers for introducing classes in v5 was to make it easier to define DSC management resources. In the latter part of the chapter, we’ll look at how this is done.

19.1. Writing classes in PowerShell

19.2. Methods in PowerShell classes

19.3. Extending existing classes

19.4. Classes, modules, using, and namespaces

19.5. Writing class-based DSC resources

19.6. Summary

sitemap