PowerShell

Calculated Properties

Introduction#

Calculated Properties in Powershell are custom derived(Calculated) properties. It lets the user to format a certain property in a way he want it to be. The calculation(expression) can be a quite possibly anything.

Display file size in KB - Calculated Properties

Let’s consider the below snippet,

Get-ChildItem -Path C:\MyFolder | Select-Object Name, CreationTime, Length

It simply output the folder content with the selected properties. Something like,

Plain Properties

What if I want to display the file size in KB ? This is where calcualted properties comes handy.

Get-ChildItem C:\MyFolder | Select-Object Name, @{Name="Size_In_KB";Expression={$_.Length / 1Kb}}

Which produces,

enter image description here

The Expression is what holds the calculation for calculated property. And yes, it can be anything!


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow