PS Calculated Properties
Calculated Properties allow you to create or modify object properties on the fly within a pipeline, typically using Select-Object.
Syntax
A calculated property is defined by a hashtable with two keys: Name (or Label) and Expression.
# Example: Transforming file size to MB
Get-ChildItem | Select-Object Name, @{
Name = "SizeMB"
Expression = { $_.Length / 1MB }
}
Common Use Cases
- Unit Conversion: Converting bytes to KB/MB/GB.
- Formatting: Truncating strings or formatting dates.
- Data Enrichment: Adding a property that is calculated from other properties (e.g.,
TotalCost = Price * Quantity). - Renaming: Changing a property name to match a target schema (e.g., changing
LengthtoFileSize).
The $_ Variable
Within the Expression block, $_ represents the current object in the pipeline. This is where the power of PowerShell's object-oriented nature shines—you have access to every property of the object while you are transforming it.