قالب وردپرس درنا توس
Home / Tips and Tricks / Calculated Properties in PowerShell Functions – CloudSavvy IT

Calculated Properties in PowerShell Functions – CloudSavvy IT



Powershell logo

Often times, a returned property of an object in PowerShell may not have the ideal name, or the value may not be in the correct format. For those cases, PowerShell has the calculated properties construct. This handy feature makes it possible to easily change the returned value during selected operations and quickly and easily return what is needed in the pipeline output. What does this look like in practice? How about taking a date format and resizing it to another format?

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

If you haven̵

7;t seen this before, it can be a pretty strange looking format to put in a Select-Object order. Read on to find out what each part means and how it can be used to easily manipulate data if needed!

Anatomy of a calculated property

A calculated property is essentially a hash table that defines how the output should search for a property. There are several keys 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 cmdlet is used for tabular output, it defines how values ​​in a column are displayed with left, center, or right as the allowed values.
  • Format string – Specifies a format string, which is a .NET format 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 specifies the maximum depth of extension per property.
  • Ascending descending – Specify the sort order of one or more properties, which are Boolean values ​​set to either $True or $False.

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

Select object and calculated properties

Mostly, Select-Object is used with calculated properties because it allows you to quickly and easily manipulate the output without changing the source data and easily pass data down the pipeline in the required format. Below is an example of what this looks like in practice! First, we’ll start with the same object we’ll be using 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 a different format. You can do it Get-Date command over the object itself, but sometimes you don’t have the option to do that. Hence, we can change the date format using a calculated property as below. With the code below, we change the date format to MM/dd/yyyy the habits 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 is mentioned NewDate was created and the date format was changed! Since this uses a script block, it can contain a lot of logic such as control statements such as switch or further searches, if necessary. Read on to learn how to run better formatted data.

Format table and calculated properties

The default view of Format-Table may leave something to be desired. So let’s see what we can do to make it a little bit better to work with. With the same object as originally defined, what does the output look like?

We can certainly make this look better! First, the alignment isn’t ideal for that Number or Enabled. Second, we must execute Number with two decimal places. Finally, let’s make it Name wider, change Date to our new format, and join us 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 make this work better.

Conclusion

Calculated properties are a unique language construct used when a fast and flexible way is needed to change pipeline output without changing upstream code. With the features available for both console display and data manipulation when passing data down a pipeline, calculated properties can be quickly integrated into any script.


Source link