PowerShell Objects
Unlike traditional text-based shells (like Bash), PowerShell is fundamentally object-oriented. Every command returns an object or a collection of objects, which can be manipulated via their properties and methods.
The Object Pipeline
In a text shell, the pipeline passes strings. In PowerShell, the pipeline passes structured data.
- Text Approach:
ls -l | grep ".txt"(Requires parsing text columns). - PowerShell Approach:
Get-ChildItem | Where-Object Extension -eq ".txt"(Filters by theExtensionproperty).
Discovery: Get-Member
The Get-Member cmdlet (alias gm) is the primary tool for inspecting objects. It reveals the object's TypeName (.NET class), Properties (data), and Methods (actions).
# Example: Inspecting a process
Get-Process | Get-Member
Creating Custom Data: PSCustomObject
The PSCustomObject is a lightweight container for structured data. It is the standard way to pass custom information through the pipeline.
$server = [PSCustomObject]@{
Name = "WebSrv01"
IP = "192.168.1.10"
Role = "Web"
}
Core Principles
- Filter Left: Filter as early as possible in the pipeline to improve performance.
- Output Objects, Not Text: Functions should return objects so they can be reused by other cmdlets.
- Everything is .NET: PowerShell is built on .NET, meaning you have access to the entire .NET library directly from the shell.