Calculated Properties in PowerShell Functions

Powershell logo

Many times a property returned by an object in PowerShell may not be named ideally or the value is not quite in the format needed. For these instances, PowerShell has the calculated properties construction. This useful feature makes it easy to change the return value during select operations and quickly and easily return what is needed in the pipelined output. In practice, what does it look like? How about taking a date format and switching to a different format?

$Object | Select-Object "Name","Date",@{
	Name = 'NewDate'
	Expression = { Get-Date $_.Date -Format "MM/dd/yyyy" }
}

If you haven’t seen it yet, it might be a rather strange format to put into a Select-Object order. Read on to find out what each part means and how it can be leveraged to easily manipulate data as needed!

Anatomy of a computed property

A calculated property is basically a hash table that defines how the output should look up a property. There are several keys that we can define to manipulate the output.

  • Name / Label – Specifies the name of the returned property, with label used as an alias.
  • Expression – The script block used to calculate the new property value.
  • Alignment – When a tabular output cmdlet is used, this will define how values ​​are displayed in a column using left, center, or right as allowed values.
  • Format string – Specifies a format string, which is a .NET format string.
  • Width – For tabular output, sets the maximum column width, which must be greater than 0.
  • Depth – Used only with the Format-Custom cmdlet, this will specify the maximum expansion depth per property.
  • Ascending Descending – Specify the sort order of one or more properties, they are Boolean values ​​set to $True or $False.

What does it look like in practice? For Select-Object you will usually only use Name and Expression. To control specific tabular output, you can use the various alignment, format, and width options.

Select-Object and calculated properties

Most of the time, Select-Object is used with calculated properties because it allows you to quickly and easily manipulate the output without having to modify the source data and easily pass the data into the pipeline in the required format. Here is an example of what it looks like in practice! First, we’ll start with the same object that we’ll use to demonstrate all of the examples below.

$Object = [PSCustomObject]@{
  'Name'    = 'Test Object'
  'Date'    = Get-Date
  'Number'  = 100
  'Enabled' = $True
  'Extra'   = @('One','Two','Three')
}

$Object

Looking at the date, it doesn’t quite match what is expected. Let’s change this to another format. You can change the Get-Date command on the object itself, but sometimes you don’t have the option to do so. Therefore, we can change the format of the date by using a calculated property as below. With the code below we change the date format to MM/dd/yyyy using the Format parameter Get-Date.

$Object | Select-Object Name,@{
  Name = 'NewDate'
  Expression = { Get-Date $_.Date -Format "MM/dd/yyyy" }
}

As you can see, a new property called NewDate has been created and the date format has changed! Since this uses a script block, you can contain a lot of logic, like control statements like switch or other research, if necessary. Read on to learn how to produce better formatted data.

Format-Table and calculated properties

The default view of Format-Table may leave something to be desired. Therefore, let’s see what we can do to improve working with it. Using the same object that was originally defined, what does the output look like?

We can improve this aspect, that’s for sure! First of all, the alignment is not ideal for Number or Enabled. Second, we should produce Number with two decimal points. Finally, let’s do Name wider, change Date to our new format and join Extra with commas. See below, how we can use calculated properties to change the view!

$Object | Format-Table @{
  Name = 'Name'
  Expression = { $_.Name }
  Width = 20
},
@{
  Name = 'Date'
  Expression = { Get-Date $_.Date -Format "MM/dd/yyyy" }
  Width = 10
},
@{
  Name = 'Number'
  Expression = { $_.Number }
  Formatstring = 'N2'
  Alignment = 'Center'
  Width = 10
},
@{
  Name = 'Enabled'
  Expression = { $_.Enabled }
  Alignment = 'Center'
  Width = 10
},
@{
  Name = 'Extra'
  Expression = { $_.Extra -Join ", " }
  Width = 30
}

Much better! It’s easier to understand the data, and you can see how all of the different possibilities with calculated properties combine to help make it work better.

Conclusion

Calculated properties are a unique language construct that is used when you need a fast and flexible way to change the output of the pipeline without modifying the code upstream. With functionality available both for viewing the console and for manipulating data when passing data further down a pipeline, calculated properties can be quickly incorporated into any script.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.