{"id":144152,"date":"2020-12-30T16:00:07","date_gmt":"2020-12-30T13:00:07","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/calculated-properties-in-powershell-functions-cloudsavvy-it\/"},"modified":"2020-12-30T16:00:07","modified_gmt":"2020-12-30T13:00:07","slug":"calculated-properties-in-powershell-functions-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/calculated-properties-in-powershell-functions-cloudsavvy-it\/","title":{"rendered":"#Calculated Properties in PowerShell Functions \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a398f040b8f3\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a398f040b8f3\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/calculated-properties-in-powershell-functions-cloudsavvy-it\/#Anatomy_of_a_Calculated_Property\" >Anatomy of a Calculated Property<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/calculated-properties-in-powershell-functions-cloudsavvy-it\/#Select-Object_and_Calculated_Properties\" >Select-Object and Calculated Properties<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/calculated-properties-in-powershell-functions-cloudsavvy-it\/#Format-Table_and_Calculated_Properties\" >Format-Table and Calculated Properties<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/calculated-properties-in-powershell-functions-cloudsavvy-it\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#Calculated Properties in PowerShell Functions \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4374\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/8fec593cd19e3b20a0773cafaaaaf848\/p\/uploads\/2020\/03\/23e4a5a4.png\" alt=\"Powershell logo\" width=\"1400\" height=\"578\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>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 <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/redirect.viglink.com?u=https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fmicrosoft.powershell.core%2Fabout%2Fabout_calculated_properties%3Fview%3Dpowershell-7.1&amp;key=204a528a336ede4177fff0d84a044482\">calculated properties<\/a> construct. This useful feature allows for modifying the returned value easily during select operations and quickly and easily return what\u2019s needed in the pipelined output. In practice, what does this look like? How about taking one date format, and changing to a different format?<\/p>\n<pre><code class=\"language-powershell\">$Object | Select-Object \"Name\",\"Date\",@{&#13;\n    Name=\"NewDate\"&#13;\n    Expression = { Get-Date $_.Date -Format \"MM\/dd\/yyyy\" }&#13;\n}&#13;\n<\/code><\/pre>\n<p>If you haven\u2019t seen this before, it can be a pretty strange looking format to be inserted in a <code>Select-Object<\/code> command. Read on to learn what each part means and how it can be leveraged to easily manipulate data as needed!<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Anatomy_of_a_Calculated_Property\"><\/span>Anatomy of a Calculated Property<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>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.<\/p>\n<ul>\n<li><strong>Name\/Label<\/strong> \u2013 Specifies the name of the returned property, with <code>label<\/code> used as an alias.<\/li>\n<li><strong>Expression<\/strong> \u2013 The <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">script<\/a> block used to calculate the new property value.<\/li>\n<li><strong>Alignment<\/strong> \u2013 When a tabular output cmdlet is used, this will define how a values are displayed in a column using <code>left<\/code>, <code>center<\/code>, or <code>right<\/code> as the allowed values.<\/li>\n<li><strong>Formatstring<\/strong> \u2013 Specifies a format string, which is a .NET formatting string.<\/li>\n<li><strong>Width<\/strong> \u2013 For tabular output, defines the maximum width of the column, which must be greater than <code>0<\/code>.<\/li>\n<li><strong>Depth<\/strong> \u2013 Only used with the <code>Format-Custom<\/code> cmdlet, this will specify the maximum depth of expansion per property.<\/li>\n<li><strong>Ascending\/Descending<\/strong> \u2013 Specify the sort order of one or more properties, these are boolean values set to either <code>$True<\/code> or <code>$False<\/code>.<\/li>\n<\/ul>\n<p>What does this look like in practice? For <code>Select-Object<\/code> you will typically only use <code>Name<\/code> and <code>Expression<\/code>. For control over specific tabular output, you can use the various alignment, format, and width options.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Select-Object_and_Calculated_Properties\"><\/span>Select-Object and Calculated Properties<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Most of the time, <code>Select-Object<\/code> 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.<\/p>\n<pre><code class=\"language-powershell\">$Object = [PSCustomObject]@{&#13;\n  'Name'    = 'Test Object'&#13;\n  'Date'    = Get-Date&#13;\n  'Number'  = 100&#13;\n  'Enabled' = $True&#13;\n  'Extra'   = @('One','Two','Three')&#13;\n}&#13;\n&#13;\n$Object&#13;\n<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8707\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/6a38d50779ab55135e8246b5f1494063\/p\/uploads\/2020\/12\/8daf4b90.png\" alt=\"\" width=\"864\" height=\"170\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Looking at the date, it doesn\u2019t quite match what is expected. Let\u2019s change this to a different format. You could modify the <code>Get-Date<\/code> command on the object itself, but sometimes you don\u2019t 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 <code>MM\/dd\/yyyy<\/code> using the <code>Format<\/code> parameter of <code>Get-Date<\/code>.<\/p>\n<pre><code class=\"language-powershell\">$Object | Select-Object Name,@{&#13;\n  Name=\"NewDate\"&#13;\n  Expression = { Get-Date $_.Date -Format \"MM\/dd\/yyyy\" }&#13;\n}&#13;\n<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8708\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/27a83b77f7324a0ecf5bf6b6fa81308a\/p\/uploads\/2020\/12\/6c2c292d.png\" alt=\"\" width=\"809\" height=\"109\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>As you can see, a new property called <code>NewDate<\/code> 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 <code>switch<\/code> or further lookups, if necessary. Read on to learn how to output better-formatted data.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Format-Table_and_Calculated_Properties\"><\/span>Format-Table and Calculated Properties<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The default view of <code>Format-Table<\/code> may leave something to be desired. Therefore, let\u2019s 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?<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8709\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/b298e060df53a648d014db78304cc0fe\/p\/uploads\/2020\/12\/6488764c.png\" alt=\"\" width=\"842\" height=\"98\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>We can make this look better, for sure! First off, the alignment is not ideal for <code>Number<\/code> or <code>Enabled<\/code>. Second, we should output <code>Number<\/code> with two decimal points. Finally, let\u2019s make <code>Name<\/code> wider, change <code>Date<\/code> to our new format, and join <code>Extra<\/code> by commas. See below, how we can use calculated properties to change the view!<\/p>\n<pre><code class=\"language-powershell\">$Object | Format-Table @{&#13;\n  Name=\"Name\"&#13;\n  Expression = { $_.Name }&#13;\n  Width = 20&#13;\n},&#13;\n@{&#13;\n  Name=\"Date\"&#13;\n  Expression = { Get-Date $_.Date -Format \"MM\/dd\/yyyy\" }&#13;\n  Width = 10&#13;\n},&#13;\n@{&#13;\n  Name=\"Number\"&#13;\n  Expression = { $_.Number }&#13;\n  Formatstring = 'N2'&#13;\n  Alignment=\"Center\"&#13;\n  Width = 10&#13;\n},&#13;\n@{&#13;\n  Name=\"Enabled\"&#13;\n  Expression = { $_.Enabled }&#13;\n  Alignment=\"Center\"&#13;\n  Width = 10&#13;\n},&#13;\n@{&#13;\n  Name=\"Extra\"&#13;\n  Expression = { $_.Extra -Join \", \" }&#13;\n  Width = 30&#13;\n}&#13;\n<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8710\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/c185902026608942cb13da0e05a20a16\/p\/uploads\/2020\/12\/9c49c320.png\" alt=\"\" width=\"797\" height=\"95\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>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.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>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.\n<\/p><\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/8706\/calculated-properties-in-powershell-functions\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#Calculated Properties in PowerShell Functions \u2013 CloudSavvy IT&#8221; 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&#8230;<\/p>\n","protected":false},"author":1,"featured_media":144153,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/03\/23e4a5a4.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-144152","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/144152","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=144152"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/144152\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/144153"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=144152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=144152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=144152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}