Classes and Advanced Functions
By the end of this lesson you'll define your own types with the class keyword — properties, constructors, methods, validation, inheritance, static members, and enums — and build advanced functions with [CmdletBinding()], mandatory and pipeline parameters, begin/process/end blocks, and comment-based help, so your scripts become genuine, professional tools.
Learn Classes and Advanced Functions in our free PowerShell course — an interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Powershell course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
A class is a cookie cutter ; each object you stamp out is a cookie. The cutter's shape (properties) and the icing instructions (methods) are defined once, so every cookie comes out consistent. A constructor is the rule for how a cookie must be made; validation rejects bad dough before it ruins a batch; inheritance is a fancier cutter built from a basic one; and a static member is the shared tally on the kitchen wall counting how many cookies the whole batch produced. An advanced function , meanwhile, is the professional-grade tool that uses these cookies on an assembly line — handling them one at a time as they come down the belt.
1. Defining a class
The class keyword (PowerShell 5+) defines a new type with properties (the data, optionally guarded by validation attributes like [ValidateNotNullOrEmpty()] ), a constructor (a method named the same as the class that runs when you create an instance), and methods (behaviour, with an optional return type). You create instances with [ClassName]::new(...) and refer to the current instance inside the class as $this . Read this worked example, then run it.
2. Inheritance, Static Members & Enums
A class can inherit from another with the colon syntax ( class Derived : Base ), gaining all its members and optionally overriding them. A static member belongs to the class itself rather than any instance — declared with static and accessed via [Class]::Member — which makes it ideal for shared counters or constants. An enum defines a fixed set of named values, giving readable names to a limited list of options.
Now you try. Fill in the three blanks using the hints in the comments, then run it.
3. Advanced Functions: [CmdletBinding()] & begin/process/end
Adding [CmdletBinding()] turns a plain function into an advanced function that behaves like a compiled cmdlet, gaining common parameters such as -Verbose and -ErrorAction . Mark a parameter [Parameter(Mandatory)] to require it and ValueFromPipeline to accept piped input. The body splits into three blocks: begin (once, before items), process (once per item ), and end (once, after items).
If your function takes pipeline input, the per-item logic must live in the process block. Put it directly in the body (no blocks) and the function behaves like one big end block — it runs once and only ever sees the last piped item. This is the single most common advanced-function bug.
A specially formatted comment block at the top of a function — with .SYNOPSIS , .DESCRIPTION , .PARAMETER , and .EXAMPLE keywords — makes Get-Help work for your function exactly as it does for built-in cmdlets. This is what turns a script into a discoverable, professional tool.
Run Get-Help Get-Cube -Full to see synopsis, description, parameters, and examples all rendered from your comment block.
No blanks this time — just a brief and an outline to keep you on track. Build it, run it, and check your output against the example in the comments. Combining a class with an advanced function is exactly how reusable, professional PowerShell tools are built.
Practice quiz
Which keyword defines a class in PowerShell?
- class
- type
- object
- struct
Answer: class. PowerShell 5+ uses the class keyword to define a class with properties and methods.
What is a constructor in a PowerShell class?
- An inherited method
- A property that holds data
- A method that runs when an instance is created
- A static field
Answer: A method that runs when an instance is created. A constructor is a special method named the same as the class that runs when you create an instance with [Class]::new().
How does one class inherit from another?
- class Dog extends Animal
- class Dog : Animal
- class Dog from Animal
- class Dog inherits Animal
Answer: class Dog : Animal. PowerShell uses a colon: 'class Dog : Animal' makes Dog inherit Animal's members.
What does a static member belong to?
- The parent only
- The pipeline
- A single instance
- The class itself, not any instance
Answer: The class itself, not any instance. Static members belong to the class itself and are accessed with [Class]::Member, not through an instance.
What does the [ValidateNotNull()] attribute do on a property?
- Rejects a null value assigned to it
- Sets a default
- Hides the property
- Makes it static
Answer: Rejects a null value assigned to it. Validation attributes like [ValidateNotNull()] enforce rules when a value is assigned to the property.
What does [CmdletBinding()] add to a function?
- A constructor
- Common parameters like -Verbose and -ErrorAction
- A return type
- Automatic inheritance
Answer: Common parameters like -Verbose and -ErrorAction. [CmdletBinding()] promotes a function to an advanced function with the common parameters of a real cmdlet.
How do you make a parameter required in an advanced function?
[Parameter(Mandatory)] forces the caller to supply a value, prompting if it is omitted.
Which block of an advanced function runs once for EACH pipeline item?
- begin
- end
- init
- process
Answer: process. The process block runs once per item that arrives from the pipeline; begin and end run once each, before and after.
Which attribute lets a parameter accept values from the pipeline?
- PipelineInput
- AcceptPipe
- ValueFromPipeline
- FromPipe
Answer: ValueFromPipeline. Marking a parameter [Parameter(ValueFromPipeline)] lets values arrive from the pipeline into a process block.
What is an enum used for?
- Storing files
- Defining a fixed set of named constant values
- Catching errors
- Joining strings
Answer: Defining a fixed set of named constant values. An enum defines a named set of constant values, giving readable names to a fixed list of options.