{"id":249,"date":"2020-05-08T18:26:42","date_gmt":"2020-05-08T17:26:42","guid":{"rendered":"http:\/\/192.168.8.14\/?p=249"},"modified":"2020-05-08T18:26:42","modified_gmt":"2020-05-08T17:26:42","slug":"tutorial-calculated-property","status":"publish","type":"post","link":"https:\/\/www.jasonstreet.com\/?p=249","title":{"rendered":"Tutorial, calculated property"},"content":{"rendered":"\n<p>Here is something that I have used for a while but had no idea what it was called. After a bit of googling, apparently its a calculated property.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is a calculated property?<\/h3>\n\n\n\n<p>A calculated property is changing the name and\/or value of an attribute as its copied to a new object or the screen.<\/p>\n\n\n\n<p>A quick example is shown below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\n$result = $object | select att1, att2, @{Name=&quot;NewAtt&quot;;Expression={$_.att3 + 1}}, att4\n<\/pre><\/div>\n\n\n<p>Here I am copying $Object and its attributes att1, att2 and att4 to $Result. I&#8217;m also copying att3 but I&#8217;m renaming it to NewAtt and adding 1 to its value. <\/p>\n\n\n\n<p>Basically the calculated property is a hash table. The fist key is the Name or Label and the second key is the Expression.<\/p>\n\n\n\n<p>If you feel lazy you can shorten the hash table to the following. I tend not to do this as I think it make the code harder to read.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\n$result = $object | select att1, att2, @{N=&quot;NewAtt&quot;;E={$_.att3 + 1}}, att4\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Using a calculated property<\/h3>\n\n\n\n<p>Starting with a simple get-childitem command.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"808\" height=\"210\" src=\"http:\/\/192.168.8.14\/wp-content\/uploads\/2020\/04\/calcprop001.png\" alt=\"\" class=\"wp-image-251\" srcset=\"https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2020\/04\/calcprop001.png 808w, https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2020\/04\/calcprop001-300x78.png 300w, https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2020\/04\/calcprop001-768x200.png 768w\" sizes=\"auto, (max-width: 808px) 100vw, 808px\" \/><\/figure>\n\n\n\n<p>I now pipe Get-ChildItem in to a select command to select what attributes I need.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"794\" height=\"153\" src=\"http:\/\/192.168.8.14\/wp-content\/uploads\/2020\/04\/calcprop002.png\" alt=\"\" class=\"wp-image-252\" srcset=\"https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2020\/04\/calcprop002.png 794w, https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2020\/04\/calcprop002-300x58.png 300w, https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2020\/04\/calcprop002-768x148.png 768w\" sizes=\"auto, (max-width: 794px) 100vw, 794px\" \/><\/figure>\n\n\n\n<p>I think Length is a silly name for filesize. I want to use Size instead. So I use.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\nGet-ChildItem | select name, mode, @{Name=&quot;SizeBytes&quot;;Expression={$_.length}}, lastwritetime\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"872\" height=\"145\" src=\"http:\/\/192.168.8.14\/wp-content\/uploads\/2020\/04\/calcprop003.png\" alt=\"\" class=\"wp-image-253\" srcset=\"https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2020\/04\/calcprop003.png 872w, https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2020\/04\/calcprop003-300x50.png 300w, https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2020\/04\/calcprop003-768x128.png 768w, https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2020\/04\/calcprop003-850x141.png 850w\" sizes=\"auto, (max-width: 872px) 100vw, 872px\" \/><\/figure>\n\n\n\n<p>But now I dont want &#8220;Size&#8221; in bytes, I want &#8220;Size&#8221; in kb. So I use.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\nGet-ChildItem | select name, mode, @{Name=&quot;SizeK&quot;;Expression={$_.length \/ 1kb}}, lastwritetime\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"875\" height=\"141\" src=\"http:\/\/192.168.8.14\/wp-content\/uploads\/2020\/04\/calcprop004.png\" alt=\"\" class=\"wp-image-254\" srcset=\"https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2020\/04\/calcprop004.png 875w, https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2020\/04\/calcprop004-300x48.png 300w, https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2020\/04\/calcprop004-768x124.png 768w, https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2020\/04\/calcprop004-850x137.png 850w\" sizes=\"auto, (max-width: 875px) 100vw, 875px\" \/><\/figure>\n\n\n\n<p>If you have not used calculated propertys before then you may think &#8220;why would I ever need this?&#8221; well if you are collecting data then you will need this sooner or later.<\/p>\n\n\n\n<p>I needed it sooner.<\/p>\n\n\n\n<p>anyhow I hope I have save people some searching.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is something that I have used for a while but had no idea what it was called. After a bit of googling, apparently its a calculated property. What is a calculated property? A calculated property is changing the name and\/or value of an attribute as its copied to a new object or the screen&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4,27],"tags":[36,37,13,38,22],"class_list":["post-249","post","type-post","status-publish","format-standard","hentry","category-powershell","category-tutorial","tag-calculated-property","tag-column","tag-powershell","tag-rename","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts\/249","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=249"}],"version-history":[{"count":2,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts\/249\/revisions"}],"predecessor-version":[{"id":255,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts\/249\/revisions\/255"}],"wp:attachment":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}