Many times a returned property from an object in PowerShell may not be ideally named, or the value is not quite in the necessary format. For those instances, PowerShell has the calculated properties construct. This useful feature allows for modifying the returned value easily during select operations and quickly and easily return what’s needed in the pipelined output. In practice, what does this look like? How about taking one date format, and changing to a different format?
$Object | Select-Object "Name","Date",@{
Name = 'NewDate'
Expression = { Get-Date $_.Date -Format "MM/dd/yyyy" }
}
If you haven’t seen this before, it can be a pretty strange looking format to be inserted in a Select-Object
command. Read on to learn what each part means and how it can be leveraged to easily manipulate data as needed!
Anatomy of a Calculated Property
A calculated property is, at its core, a hashtable that defines how the output should look for 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 a values are displayed in a column using
left
,center
, orright
as the allowed values. - Formatstring – Specifies a format string, which is a .NET formatting string.
- Width – For tabular output, defines the maximum width of the column, which must be greater than
0
. - Depth – Only used with the
Format-Custom
cmdlet, this will specify the maximum depth of expansion per property. - Ascending/Descending – Specify the sort order of one or more properties, these are boolean values set to either
$True
or$False
.
What does this look like in practice? For Select-Object
you will typically only use Name
and Expression
. For control over 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 as this lets you quickly and easily manipulate the output without having to modify the source data and conveniently pass data down the pipeline in the needed format. Below is an example of what this looks like in practice! First, we will start with the same object that we will use to demonstrate all 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 a different format. You could modify the Get-Date
command on the object itself, but sometimes you don’t have the ability to do that. Therefore we can change up the date format using a calculated property like below. With the below code, we are changing the date format to MM/dd/yyyy
using the Format
parameter of 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 is using a script block, you can contain a lot of logic within, such as control statements like switch
or further lookups, if necessary. Read on to learn how to output 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 make it a bit better to work with. Using the same object as originally defined, what does the output look like?
We can make this look better, for sure! First off, the alignment is not ideal for Number
or Enabled
. Second, we should output Number
with two decimal points. Finally, let’s make Name
wider, change Date
to our new format, and join Extra
by 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! This is easier to understand the data and you can see how all the different possibilities with calculated properties come together to help make this work better.
Conclusion
Calculated properties are a unique language construct that come in use when needing a fast and flexible way to modify pipeline output without modifying upstream code. With the features available both for console display and for data manipulation when passing data further down a pipeline, calculated properties can be quickly integrated into any script.
The post Calculated Properties in PowerShell Functions – CloudSavvy IT appeared first on TechFans.