liveBook · Manning

Class

As explained in Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

A class is a .NET type that defines the members and behavior of objects or provides class-level functionality.

A class is a .NET type: a definition of functioning software that describes how to interact with it. An instance is a concrete implementation of that class, such as a particular running process. Classes contain members, including properties, methods, and events. section 25.1.1

Use these distinctions when exploring .NET objects from PowerShell:

  • Properties describe what an object represents; some can be changed, while others are read-only. section 25.1.1
  • Methods perform actions and can accept arguments, much like command parameters. section 25.1.1
  • Events signal that something happened to an instance, although PowerShell offers limited event-driven support. section 25.1.1
  • Abstract classes can provide functionality without a concrete instance; static members are accessed directly through the class. section 25.1.1 section 25.2

Call a class from PowerShell

PowerShell identifies .NET types with square brackets and accesses static members with double colons. For example, [Math]::Abs(-5) calls the static Abs method without creating a Math instance. Instance-based classes work differently: load the required assembly, create an object with New-Object, and call its method, as with a SpeechSynthesizer object's Speak method. section 25.2 section 25.6

[Math]::Abs(-5)

See the compact PowerShell example for invoking a static class member. section 25.2

How Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools covers it

How deep it goes
Defines types, instances, members, static access, and assembly loading, but stops short of detailed class implementation.
Examples
Demonstrates Math.Abs, SpeechSynthesizer, assembly loading, speech output, and PowerShell wrappers for .NET functionality.
What it assumes
Teaches class terminology from zero while using basic PowerShell conventions such as type brackets, Get-Member, and New-Object.
What you can do after
Apply it via PowerShell to locate .NET classes, call static members, create instances, and reuse .NET functionality in scripts.
How it connects
Connects classes with types, instances, members, properties, methods, events, namespaces, assemblies, and PowerShell.

Also explained in

Compare how 21 books explain class →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools. The section links point to the parts of the book it draws on.