{"id":1246,"date":"2021-01-20T14:41:44","date_gmt":"2021-01-20T20:41:44","guid":{"rendered":"http:\/\/ninmonkeys.com\/blog\/?p=1246"},"modified":"2021-02-20T20:40:24","modified_gmt":"2021-02-21T02:40:24","slug":"easy-way-to-cache-results-on-the-command-line-power-shell-tip","status":"publish","type":"post","link":"https:\/\/ninmonkeys.com\/blog\/2021\/01\/20\/easy-way-to-cache-results-on-the-command-line-power-shell-tip\/","title":{"rendered":"Easy way to cache results on the Command Line  | Power Shell Tip"},"content":{"rendered":"\n\n\n<p>Sometimes you&#8217;ll need to run a command with the same input with different logic. <br>This can be a hassle using a slow command like <code class=\"\" data-line=\"\">Get-ADUser<\/code> or <code class=\"\" data-line=\"\">Get-ChildItem<\/code> on a lot of files like <code class=\"\" data-line=\"\">~<\/code> (Home) with <code class=\"\" data-line=\"\">-Depth<\/code> \/ <code class=\"\" data-line=\"\">-Recurse<\/code><\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-powershell\" data-line=\"\">ls ~ -Depth 4 | Format-Table Name<\/code><\/pre>\n\n\n\n<h2>PowerShell 7.0+<\/h2>\n\n\n\n<p><a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/scripting\/whats-new\/what-s-new-in-powershell-70?view=powershell-7\">Powershell 7<\/a> added the <a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.core\/about\/about_operators?view=powershell-7#ternary-operator--if-true--if-false\">Ternary Operator<\/a>, and several operators for handling <code class=\"\" data-line=\"\">$null<\/code> values.<\/p>\n\n\n\n<p>All of these examples will only run <code class=\"\" data-line=\"\">Get-ChildItem<\/code> the first time. Any future calls are cached.<\/p>\n\n\n\n<h3>Null-Coalesce <code class=\"\" data-line=\"\">??=<\/code> Assignment Operator<\/h3>\n\n\n\n<p>This is my favorite on the Command line. The <code class=\"\" data-line=\"\">RHS<\/code> (Right Hand Side) skips evaluation if the left side is not <code class=\"\" data-line=\"\">$null<\/code><\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-powershell\" data-line=\"\">$AllFiles ??= ls ~ -Depth 4<\/code><\/pre>\n\n\n\n<h3>Using the Null-Coalesce <code class=\"\" data-line=\"\">??<\/code> Operator<\/h3>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-powershell\" data-line=\"\">$AllFiles = $AllFiles ?? ( ls ~ -Depth 4  )<\/code><\/pre>\n\n\n\n<h3>Ternary Operator <code class=\"\" data-line=\"\">? whenTrue : WhenFalse<\/code><\/h3>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-powershell\" data-line=\"\">$allFiles = $allFiles ? $allFiles : ( ls ~ -Depth 4 )<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"494\" height=\"215\" src=\"https:\/\/ninmonkeys.com\/blog\/wp-content\/uploads\/2021\/01\/image-21.png\" alt=\"\" class=\"wp-image-1251\" srcset=\"https:\/\/ninmonkeys.com\/blog\/wp-content\/uploads\/2021\/01\/image-21.png 494w, https:\/\/ninmonkeys.com\/blog\/wp-content\/uploads\/2021\/01\/image-21-300x131.png 300w\" sizes=\"(max-width: 494px) 100vw, 494px\" \/><\/figure>\n\n\n\n<h2><code class=\"\" data-line=\"\">Windows PowerShell<\/code> and <code class=\"\" data-line=\"\">Powershell &lt; 7<\/code><\/h2>\n\n\n\n<p>Windows Powershell can achieve the same effect with an <code class=\"\" data-line=\"\">if statement<\/code><\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-powershell\" data-line=\"\">if(! $AllFiles) { $AllFiles = ls ~ -Depth 4 }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you&#8217;ll need to run a command with the same input with different logic. This can be a hassle using a slow command like Get-ADUser or Get-ChildItem on a lot of files like ~ (Home) with -Depth \/ -Recurse PowerShell 7.0+ Powershell 7 added the Ternary Operator, and several operators for handling $null values. All [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1266,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[66,18,40,14],"tags":[29,4,15,22],"_links":{"self":[{"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/posts\/1246"}],"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=1246"}],"version-history":[{"count":21,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/posts\/1246\/revisions"}],"predecessor-version":[{"id":1270,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/posts\/1246\/revisions\/1270"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/media\/1266"}],"wp:attachment":[{"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/media?parent=1246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/categories?post=1246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ninmonkeys.com\/blog\/wp-json\/wp\/v2\/tags?post=1246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}