{"id":4995,"date":"2012-09-10T00:01:00","date_gmt":"2012-09-10T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/09\/10\/increase-powershell-command-history-to-the-max-almost\/"},"modified":"2012-09-10T00:01:00","modified_gmt":"2012-09-10T00:01:00","slug":"increase-powershell-command-history-to-the-max-almost","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/increase-powershell-command-history-to-the-max-almost\/","title":{"rendered":"Increase PowerShell Command History to the Max (Almost)"},"content":{"rendered":"<p><b>Summary<\/b>: In Windows PowerShell command history, you may run into the console history limit.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. While I was in Cincinnati, Ohio for their first Windows PowerShell User Group meeting, I had a chance to meet a lot of my old friends. Later that week when I was teaching a Windows PowerShell workshop for a customer, I had a chance to meet other old friends. You might say that I was geeking out on seeing so many of my old friends&mdash;in fact, I was a bit worried that I would overflow my personal history buffer. Luckily, in Windows PowerShell&nbsp;3.0, overflowing the history buffer is a bit difficult to do.<\/p>\n<h2>Playing around with Windows PowerShell 3.0 history settings<\/h2>\n<p>In Windows PowerShell&nbsp;3.0 the <b>MaximumHistoryCount <\/b>is set to 4096. I find this value by using the <b>Get-Variable <\/b>cmdlet. This command is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Variable -Name MaximumHistoryCount<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Value<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">MaximumHistoryCount&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4096<\/p>\n<p>At 4096, it is unlikely that I will ever need to increase the storage space. In Windows PowerShell&nbsp;1.0, it was set to 64, and I frequently was overwriting my command history. I generally do not keep the same Windows PowerShell console session open long enough to type 4096 commands&mdash;so I have never overwritten that history buffer. But what if I feel the need to increase my command history? What is the biggest value I can assign? One way to find out is to simply run a command and see what happens. Using one of my top ten favorite Windows PowerShell tricks, I use the range operator and begin assigning new values. Here is the command I use.<\/p>\n<p style=\"padding-left: 30px\">1..50000 | % {Set-Variable -Name MaximumHistoryCount -Value $_ }<\/p>\n<p>What I am looking for is an error to raise that states I have exceeded the allowed value for the variable. This is exactly what happens, and I press CTRL-C to break the command and prevent further errors from appearing in the Windows PowerShell console. The command and the error that appears when the command runs are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2046.HSG-9-10-12-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2046.HSG-9-10-12-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>A better approach to finding maximum values that are permitted for a variable involves using the <b>Get-Variable<\/b> cmdlet. In the following command, I return information about the <b>MaximumHistoryCount<\/b> variable by piping the output to the <b>Format-List<\/b> cmdlet.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Variable MaximumHistoryCount | fl *<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : MaximumHistoryCount<\/p>\n<p style=\"padding-left: 30px\">Description : The maximum number of history objects to retain in a session.<\/p>\n<p style=\"padding-left: 30px\">Value&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 4096<\/p>\n<p style=\"padding-left: 30px\">Visibility&nbsp; : Public<\/p>\n<p style=\"padding-left: 30px\">Module&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">ModuleName&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">Options&nbsp;&nbsp;&nbsp;&nbsp; : None<\/p>\n<p style=\"padding-left: 30px\">Attributes&nbsp; : {System.Management.Automation.ValidateRangeAttribute}<\/p>\n<p>I see that the <b>Attributes<\/b> property contains an object. To investigate the <b>Attributes<\/b> property, I pipe the results of <b>Get-Variable<\/b> to the <b>Select-Object<\/b> cmdlet, and I use the <b>ExpandProperty<\/b> parameter. This technique is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Variable MaximumHistoryCount | Select-Object -ExpandProperty attributes<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MinRange&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MaxRange TypeId<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8211; &#8212;&#8212;<\/p>\n<p style=\"padding-left: 30px\">1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 32767 System.Management.Automa&#8230;<\/p>\n<p>The <b>MaxRange<\/b> property tells me that the maximum allowed value for <b>MaximumHistoryCount<\/b> automatic variable is 32767. Interestingly enough, the error shown in the previous image occurs when I attempt to set the value to 32768, and I could assume that 32767 was the most that I could store. Now I know for certain.<\/p>\n<p>Join me tomorrow when I will talk about more cool Windows PowerShell stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: In Windows PowerShell command history, you may run into the console history limit. Microsoft Scripting Guy, Ed Wilson, is here. While I was in Cincinnati, Ohio for their first Windows PowerShell User Group meeting, I had a chance to meet a lot of my old friends. Later that week when I was teaching a [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[364,3,45],"class_list":["post-4995","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-powershell-3-0","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: In Windows PowerShell command history, you may run into the console history limit. Microsoft Scripting Guy, Ed Wilson, is here. While I was in Cincinnati, Ohio for their first Windows PowerShell User Group meeting, I had a chance to meet a lot of my old friends. Later that week when I was teaching a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4995","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=4995"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4995\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=4995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}