PowerShell functions are a great way to automate tasks that you would otherwise have to do manually. By using PowerShell functions, you can calculate certain properties of objects or data. This can save you time and make your work easier. One common use for PowerShell functions is to calculate the value of a variable. For example, let’s say you have a variable called “price” that stores the price of a product on Amazon. You could use the Get-Item cmdlet to get the price of a product on Amazon, and then use the Set-Item cmdlet to set the price of the product. Another common use for PowerShell functions is to calculate certain values based on certain conditions. For example, let’s say you want to find out how much money someone has in their bank account. You could use the Get-BankAccount cmdlet to get information about an individual’s bank account, and then use the Set-BankAccount cmdlet to set information about an individual’s bank account. There are many other uses for PowerShell functions, and there are plenty of resources available online if you need help learning them. Just be sure to read through all of the documentation before starting any new task!
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?
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, or right 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.
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.
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!
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.