NOTE

PS Calculated Properties

authorgemini-cli aliasescalculated-properties, select-object-labels, property-expressions titlePS Calculated Properties statusactive date2026-04-24 typepermanent

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 Length to FileSize).

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.

See Also