{"id":980,"date":"2020-12-17T17:38:34","date_gmt":"2020-12-17T23:38:34","guid":{"rendered":"http:\/\/ninmonkeys.com\/blog\/?p=980"},"modified":"2020-12-21T13:34:06","modified_gmt":"2020-12-21T19:34:06","slug":"reusable-calculated-properties-in-powershell","status":"publish","type":"post","link":"https:\/\/ninmonkeys.com\/blog\/2020\/12\/17\/reusable-calculated-properties-in-powershell\/","title":{"rendered":"Reusable Calculated Properties in PowerShell"},"content":{"rendered":"\n<p>The function<code class=\"\" data-line=\"\"> Label<\/code> is from the module: <a href=\"https:\/\/github.com\/ninmonkey\/Ninmonkey.Console\/blob\/master\/public\/Write-ConsoleLabel.ps1\">github.com\/Ninmonkey.Console<\/a><\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-powershell\" data-line=\"\">$basePath = &#039;C:\\Program Files&#039;\n$calcProp = @{}\n$calcProp.FileSize = @{\n    n         = &#039;Size&#039;\n    e         = { $_.Length | Format-FileSize }\n    Alignment = &#039;right&#039;\n}\n# relative path defaults to relative your current location, so I override it\n$calcProp.RelativePath = @{\n    n = &#039;RelativePath&#039;\n    e = {\n        Push-Location $baseBath\n        $_.FullName | Resolve-Path -Relative\n        Pop-Location\n    }\n}\n$calcProp.ColorizedName = @{\n    n = &#039;ColorName&#039;\n    e = {\n        $Color = ($_ | Test-IsDirectory) ? &#039;blue&#039; : &#039;green&#039;\n        Label $_.Name -fg $Color -Separator &#039;&#039;\n    }\n}\n\n$DefaultPropList = &#039;Name&#039;, $calcProp.ColorizedName, $calcProp.RelativePath\n\nLabel &#039;basePath&#039; $basePath\n\n$files = Get-ChildItem $basePath -Depth 2\n$files | Format-Table Name, Length, $calcProp.FileSize, $calcProp.RelativePath\n\n$files | Format-Table  $calcProp.FileSize, $calcProp.ColorizedName\n\n# Using pre-declared property lists, which may include scriptblocks like $calcProp.FileSize\n$files | Format-Table -Property $DefaultPropList<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The function Label is from the module: github.com\/Ninmonkey.Console<\/p>\n","protected":false},"author":1,"featured_media":982,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[50,18,47],"tags":[51,30,35,4,22],"_links":{"self":[{"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/posts\/980"}],"collection":[{"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/comments?post=980"}],"version-history":[{"count":9,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/posts\/980\/revisions"}],"predecessor-version":[{"id":1002,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/posts\/980\/revisions\/1002"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/media\/982"}],"wp:attachment":[{"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/media?parent=980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/categories?post=980"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/tags?post=980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}