The release of Powershell 5 added classes to help simplify the way we write Powershell code. Classes brings Object-Oriented Programming to Powershell, it helps us encapsulate and localize variables and methods by creating objects and promote code reusability. Current Powershell scripts have a few common problems like code duplication and encapsulation issues were you are forced to clear or rename variables. We can improve on this with the use of classes. Scripts that have hundreds of lines of code, can be split into modules using OOP.
Lets create a simple script to demonstrate OOP in powershell.
First you need to create a module for the class. So create a new file and call it createFile.psm1 then add the code below.
The next step is to import this module in your ps1 script file. Create a new file in the same directory and call it main.ps1 or anything you want. Add the below code and save the files.
When the above code is run you will see the below output.
This is just a simple example using classes in powershell to get you started. You can go here for more details, https://xainey.github.io/2016/powershell-classes-and-concepts/
Thanks for reading!