PowerShell How-To

Using PowerShell's Calculated Properties

This guide will walk you through changing property names and values for the Select-Object cmdlet.

One of the big advantages of using PowerShell as a scripting language is its use of objects. Since everything is an object in PowerShell, we can take advantage of structured data by calling properties instead of parsing strings. However, the property names and values that are returned by various sources are not always exactly how we'd like. In this article, we're going to cover how to dynamically change property names and values coming from commands using the Select-Object cmdlet.

To get started, let's first cover what Select-Object does in the first place. Select-Object is a commonly used PowerShell cmdlet that allows you to change the properties that are returned from a command or change how they are displayed. For example, let's say that you need to display a listing of all files in a folder. You'd do this with Get-ChildItem.

[Click on image for larger view.] Figure 1.

You can see that this might return objects with lots of properties. If just running Get-ChildItem by itself, it shows a few different properties.

[Click on image for larger view.] Figure 2.

Maybe I just want to see the file name and directory without anything else. To do this, I could use Select-Object with it's Property parameter.

[Click on image for larger view.] Figure 3.

This is basic usage of Select-Object but what if I need a property for my report that Get-ChildItem does not return by default? I can use calculated properties. Calculated properties allow you to create new properties or change existing ones at run-time. This is done by passing a specially crafted hashtable to the Property parameter rather than a static property name.

To modify the output with calculated properties requires a hashtable with a Name and an Expression key. The name key is the property name and the Expression key is a scriptblock that will be executed as Select-Object receives input. Below is an example of a calculated property hashtable.

@{ Name = '';  Expression = {}}

Let's say I need to add a property to the output of Get-ChildItem called 'FromComputer' that specifies the computer these files came from. I can do this by using FromComputer as the Name value and some code to dynamically grab the computer name.

Creating this hashtable table and using it for the Property parameter of Select-Object would look like this:

Get-ChildItem |  Select-Object -Property Name,Directory,@{ Name = 'FromComputer'; Expression = {  hostname }}

You can see that I can have a combination of both static property names and calculated properties as well.

[Click on image for larger view.] Figure 4.

This is running the hostname command for every object that Get-ChildItem returns. Because of this behavior, calculated properties can sometime degrade performance considerably depending on the code that's being executed inside of the scriptblock.

Also, Select-Object can be used to merely modify the original values a little bit by parsing strings. In some cases, you may just want to tweak the output a little bit. Perhaps you want to wrap the file names in parentheses for some reason from our example above. To do that simply requires concatenating the parentheses as shown below.

Get-ChildItem |  Select-Object -Property Name,Directory,@{ Name = 'FromComputer'; Expression = {  hostname }},@{Name = 'WithParens';Expression = {"( $($_.Name) )"}}
[Click on image for larger view.] Figure 5.

Calculated properties are a quick way to manipulate command output to return just about anything you'd like. By being able to use a scriptblock's output for the property values, calculated properties are flexible and provide a consistent way to get the desired output from any command.

About the Author

Adam Bertram is a 20-year veteran of IT. He's an automation engineer, blogger, consultant, freelance writer, Pluralsight course author and content marketing advisor to multiple technology companies. Adam also founded the popular TechSnips e-learning platform. He mainly focuses on DevOps, system management and automation technologies, as well as various cloud platforms mostly in the Microsoft space. He is a Microsoft Cloud and Datacenter Management MVP who absorbs knowledge from the IT field and explains it in an easy-to-understand fashion. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn or follow him on Twitter at @adbertram or the TechSnips Twitter account @techsnips_io.


comments powered by Disqus
Most   Popular