Calculated Properties in PowerShell Capabilities – CloudSavvy IT

Posted on

Powershell logo

Many instances a returned property from an object in PowerShell will not be ideally named, or the worth will not be fairly within the obligatory format. For these situations, PowerShell has the calculated properties assemble. This handy function permits for modifying the returned worth simply throughout choose operations and shortly and simply return what’s wanted within the pipelined output. In follow, what does this appear to be? How about taking one date format, and altering to a unique format?

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

If you happen to haven’t seen this earlier than, it may be a reasonably unusual wanting format to be inserted in a Choose-Object command. Learn on to study what every half means and the way it may be leveraged to simply manipulate information as wanted!

Anatomy of a Calculated Property

A calculated property is, at its core, a hashtable that defines how the output ought to search for a property. There are a number of keys that we will outline to govern the output.

  • Title/Label – Specifies the identify of the returned property, with label used as an alias.
  • Expression – The script block used to calculate the brand new property worth.
  • Alignment – When a tabular output cmdlet is used, this can outline how a values are displayed in a column utilizing left, middle, or proper because the allowed values.
  • Formatstring – Specifies a format string, which is a .NET formatting string.
  • Width – For tabular output, defines the utmost width of the column, which have to be higher than 0.
  • Depth – Solely used with the Format-Customized cmdlet, this can specify the utmost depth of growth per property.
  • Ascending/Descending – Specify the type order of a number of properties, these are boolean values set to both $True or $False.

What does this appear to be in follow? For Choose-Object you’ll sometimes solely use Title and Expression. For management over particular tabular output, you need to use the assorted alignment, format, and width choices.

Choose-Object and Calculated Properties

More often than not, Choose-Object is used with calculated properties as this allows you to shortly and simply manipulate the output with out having to switch the supply information and conveniently cross information down the pipeline within the wanted format. Under is an instance of what this appears like in follow! First, we are going to begin with the identical object that we’ll use to reveal all of the examples under.

$Object = [PSCustomObject]@{
  'Title'    = 'Check Object'
  'Date'    = Get-Date
  'Quantity'  = 100
  'Enabled' = $True
  'Further'   = @('One','Two','Three')


Wanting on the date, it doesn’t fairly match what is predicted. Let’s change this to a unique format. You may modify the Get-Date command on the item itself, however typically you don’t have the flexibility to do this. Subsequently we will change up the date format utilizing a calculated property like under. With the under code, we’re altering the date format to MM/dd/yyyy utilizing the Format parameter of Get-Date.

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

As you may see, a brand new property known as NewDate has been created and the date format has modified! Since that is utilizing a script block, you may include a number of logic inside, comparable to management statements like swap or additional lookups, if obligatory. Learn on to learn to output better-formatted information.

Format-Desk and Calculated Properties

The default view of Format-Desk might go away one thing to be desired. Subsequently, let’s see what we will do to make it a bit higher to work with. Utilizing the identical object as initially outlined, what does the output appear to be?

We will make this look higher, for certain! First off, the alignment will not be supreme for Quantity or Enabled. Second, we must always output Quantity with two decimal factors. Lastly, let’s make Title wider, change Date to our new format, and be part of Further by commas. See under, how we will use calculated properties to vary the view!

$Object | Format-Desk @{
  Expression = { $_.Title }
  Width = 20
  Expression = { Get-Date $_.Date -Format "MM/dd/yyyy" }
  Width = 10
  Expression = { $_.Quantity }
  Formatstring = 'N2'
  Width = 10
  Expression = { $_.Enabled }
  Width = 10
  Expression = { $_.Further -Be a part of ", " }
  Width = 30

A lot better! That is simpler to grasp the information and you may see how all of the completely different potentialities with calculated properties come collectively to assist make this work higher.


Calculated properties are a novel language assemble that are available in use when needing a quick and versatile approach to modify pipeline output with out modifying upstream code. With the options obtainable each for console show and for information manipulation when passing information additional down a pipeline, calculated properties could be shortly built-in into any script.

Source link

Gravatar Image
I love to share everything with you

Leave a Reply

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