{"id":506,"date":"2008-07-22T06:00:04","date_gmt":"2008-07-22T14:00:04","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/2008\/07\/22\/fun-with-history\/"},"modified":"2008-07-22T04:06:12","modified_gmt":"2008-07-22T12:06:12","slug":"fun-with-history","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2008\/07\/22\/fun-with-history\/","title":{"rendered":"Fun with History"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" height=\"135\" align=\"left\" width=\"240\" src=\"http:\/\/route66.backroadsplanet.com\/imagescars\/c-mater.jpeg\" style=\"margin: 0px 5px 5px 0px;\" alt=\"\" \/> In the Pixar film <a target=\"_blank\" href=\"http:\/\/www.pixar.com\/featurefilms\/cars\/\">Cars<\/a>, Mater demonstrates to Lightning McQueen how to drive backwards. Lightning is astounded. Mater explains, &quot;Don&#8217;t need to know where I&#8217;m going as long as I know where I&#8217;ve been.&quot; Probably not the exact quote but the point is if you know what you&#8217;ve done or where you&#8217;ve been it makes it very easy to know where you&#8217;re going or what to do next. In PowerShell you can take advantage of the shell&#8217;s history objects to speed up what you want to do next.<\/p>\n<p>Every PowerShell expression is stored as a history object which you can see by running Get-History.&nbsp; Or I like to use the alias &#8216;h&#8217;. You&#8217;ll typically see the last 30 or so PowerShell expressions. The total size of your history buffer is controlled by the $MaximumHistoryCount variable which has a default value of 64.&nbsp; In my PowerShell profile I increase it to 256.<\/p>\n<p>$maximumhistorycount=256<\/p>\n<p>To execute any past command use the Invoke-History cmdlet specifying the number of the history item you wish to re-execute. I prefer to use the alias &#8216;r&#8217;. The command will run immediately. There is no provision for bringing up the expression and editing it first. Although you can use -whatif to see what command would execute.<\/p>\n<p>If you want to see earlier history items, you can either specify a specific history number<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td bgcolor=\"blue\">\n<pre><font color=\"#ffffff\">PS C:\\&gt; h 35<br \/><br \/>  Id CommandLine<br \/>  -- -----------<br \/>  35 dir | remove-item -whatif -outvariable foofiles<br \/><br \/><br \/>PS C:\\&gt;<\/font><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Or you can specify a range:<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td bgcolor=\"blue\">\n<pre><font color=\"#ffffff\">PS C:\\&gt; h (35..40)<br \/><br \/>  Id CommandLine<br \/>  -- -----------<br \/>  35 dir | remove-item -whatif -outvariable foofiles<br \/>  36 $foofiles<br \/>  37 dir *.jdh<br \/>  38 dir *.jdh | remove-item -whatif -outvariable foofiles<br \/>  39 $foofiles<br \/>  40 copy *.jdh *.abc<br \/><br \/><br \/>PS C:\\&gt;<\/font><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>To see the last X number of items use the -count parameter:<\/p>\n<p>PS C:\\&gt; h -count 60<\/p>\n<p>Finally, the history object itself has some additional properties you might find helpful. Here&#8217;s an example:<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td bgcolor=\"blue\">\n<pre><font color=\"#ffffff\">PS C:\\&gt; h 101 | select *<br \/><br \/><br \/>Id                 : 101<br \/>CommandLine        : $f=gci &quot;c:\\users\\jeff\\documents&quot; -recurse<br \/>ExecutionStatus    : Completed<br \/>StartExecutionTime : 7\/18\/2008 12:42:54 PM<br \/>EndExecutionTime   : 7\/18\/2008 12:42:57 PM<br \/><\/font><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>I can see that it took 3 seconds to run Get-ChildItem on my Documents folder. Or if I really want to get granular:<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td bgcolor=\"blue\">\n<pre><font color=\"#ffffff\">PS C:\\&gt; (h 101).EndExecutiontime - (h 101).StartExecutiontime<br \/><br \/><br \/>Days              : 0<br \/>Hours             : 0<br \/>Minutes           : 0<br \/>Seconds           : 3<br \/>Milliseconds      : 285<br \/>Ticks             : 32853285<br \/>TotalDays         : 3.80246354166667E-05<br \/>TotalHours        : 0.00091259125<br \/>TotalMinutes      : 0.054755475<br \/>TotalSeconds      : 3.2853285<br \/>TotalMilliseconds : 3285.3285<br \/><\/font><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>So the next time you&#8217;re wondering where you might be going in PowerShell, take a look at where you&#8217;ve been.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the Pixar film Cars, Mater demonstrates to Lightning McQueen how to drive backwards. Lightning is astounded. Mater explains, &#8220;Don&#8217;t need to know where I&#8217;m going as long as I know where I&#8217;ve been.&#8221; Probably not the exact quote but the point is if you know what you&#8217;ve done or where you&#8217;ve been it makes it very easy to know where you&#8217;re going or what to do next. In PowerShell you can take advantage of the shell&#8217;s history objects to speed up what you want to do next.<\/p>\n","protected":false},"author":3,"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":[25],"tags":[150,239,238,240,28],"class_list":["post-506","post","type-post","status-publish","format-standard","hentry","category-windows-powershell","tag-cmdlet","tag-get-history","tag-history","tag-invoke-history","tag-powershell"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/506","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=506"}],"version-history":[{"count":3,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/506\/revisions"}],"predecessor-version":[{"id":507,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/506\/revisions\/507"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=506"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=506"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}