{"id":4181,"date":"2013-02-11T00:01:00","date_gmt":"2013-02-11T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/02\/11\/clean-up-your-powershell-history-to-remove-error-entries\/"},"modified":"2013-02-11T00:01:00","modified_gmt":"2013-02-11T00:01:00","slug":"clean-up-your-powershell-history-to-remove-error-entries","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/clean-up-your-powershell-history-to-remove-error-entries\/","title":{"rendered":"Clean Up Your PowerShell History to Remove Error Entries"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary:<\/strong><span style=\"font-size: 12px\"> Microsoft Scripting Guy, Ed Wilson, shows how to remove all error commands from your Windows PowerShell command history.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. One of the great things about being involved with a Windows PowerShell user group is all the great people I meet and all the cool ideas that arise. It seems that every time I attend a Windows PowerShell user group meeting, I come away with at least one idea for a Hey, Scripting Guy! Blog post. (The <a href=\"http:\/\/powershellgroup.org\/charlotte.nc\" target=\"_blank\">Charlotte PowerShell User Group<\/a> meets on the first Thursday of the month at the Microsoft office in Charlotte, North Carolina, in the United States.)<\/p>\n<p><strong>Note<\/strong> You can see if there is a Windows PowerShell user group that meets in your area by going to the <a href=\"http:\/\/powershellgroup.org\/\" target=\"_blank\">PowerShellGroup.org webpage<\/a>. If you would like to start a Windows PowerShell user group in your area, you may want to <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/community\/\" target=\"_blank\">take a look at these Hey, Scripting Guy! Blog posts for ideas on how to start<\/a>.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0268.hsg-2-11-13-00.jpg\"><img decoding=\"async\" style=\"float: left;margin: 10px\" title=\"Image of NYC Windows PowerShell user group logo\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0268.hsg-2-11-13-00.jpg\" alt=\"Image of NYC Windows PowerShell user group logo\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Double Note<\/strong> &nbsp;&nbsp;Tonight, February 11, 2013, at 6:00 P.M. in New York City is the <a href=\"http:\/\/powershellgroup.org\/nyc\" target=\"_blank\">NYC Windows PowerShell User Group meeting<\/a>. They will be sharing their favorite Windows PowerShell tips and tricks, and I will publish them on the Hey, Scripting Guy! Blog. This will be cool. If you are anywhere near the Sixth Avenue Microsoft office, you should check it out. You can register (for free) via the link above.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h2>Examining the error object<\/h2>\n<p><span>Anyway, Microsoft PowerShell MVP <a href=\"http:\/\/social.technet.microsoft.com\/Search\/en-US?query=jim%20christopher&amp;beta=0&amp;rn=Hey%2c+Scripting+Guy!+Blog&amp;rq=site:blogs.technet.com\/b\/heyscriptingguy\/&amp;ac=8\" target=\"_blank\">Jim Christopher<\/a> (the Charlotte Windows PowerShell user group president) created a lab using a <a href=\"http:\/\/psqlite.codeplex.com\/\" target=\"_blank\">way cool SQLite provider he wrote<\/a>. This turned into a great hands-on lab for working with providers. During the course of the wrap up and summary, I suggested we look though the <strong>$error<\/strong> object and commented on the fact that if you pipe the object to <strong>Format-List *<\/strong> and use the <strong>&ndash;FORCE<\/strong> parameter, additional details appear.&nbsp;<\/span><\/p>\n<p>An example of this technique is shown using <strong>$error[0]<\/strong>, which is always the most recent error generated.<\/p>\n<p style=\"padding-left: 30px\">$error[0] | Format-List * -Force<\/p>\n<p>The command and its associated output are shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0257.hsg-2-11-13-01.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0257.hsg-2-11-13-01.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>So, the <strong>InvocationInfo<\/strong><em> <\/em>property contains an object. I can access the object directly, as shown here.<\/p>\n<p style=\"padding-left: 30px\">$Error[0].InvocationInfo<\/p>\n<p>The command and the output associated with the command are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0245.hsg-2-11-13-02.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0245.hsg-2-11-13-02.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>Now, if I use the <strong>Get-History<\/strong> cmdlet to display my command history, I can immediately see several errors are retained in my history. This makes it a real pain when I am using the Up Arrow key through a series of commands. The <strong>Get-History<\/strong> command and the associated output are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3542.hsg-2-11-13-03.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3542.hsg-2-11-13-03.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<h2>Ok, now I clear the history entries that have errors<\/h2>\n<p>To clear all of the error-causing commands from my command history, I first pipe the <strong>$error<\/strong> object to the <strong>Foreach-Object<\/strong> cmdlet (<strong>%<\/strong> is an alias for the <strong>Foreach-Object<\/strong> cmdlet). Inside the script block for the <strong>Foreach-Object<\/strong> cmdlet, I pipe the history IDs to the <strong>Where-Object<\/strong> (<strong>?<\/strong> is an alias for the <strong>Where-Object<\/strong>) where I look for a history ID that is greater than 0.<\/p>\n<p>Once I find those, I pipe the history IDs once again to a <strong>Foreach-Object<\/strong> cmdlet (still using <strong>%<\/strong> as an alias). Inside the <strong>Foreach-Object<\/strong> cmdlet, I use the <strong>Clear-History<\/strong> cmdlet to remove the error-inducing commands from my history stack. The complete command is shown here.<\/p>\n<p style=\"padding-left: 30px\">$Error | % { $_.invocationinfo.historyid} | ? {$_ -gt 0} | % {clear-history -id $_}<\/p>\n<p>Now when I check my command history, I see that that most of the offending commands were successfully removed from the history. This is shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8838.hsg-2-11-13-04.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8838.hsg-2-11-13-04.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>But, some of the errors&mdash;those related to calling a static method on a non-existent type&mdash;did not get deleted. The reason is that those types of errors do not write the proper history index&mdash;instead they show up with a <strong>-1<\/strong> for the HistoryID, as shown here.<\/p>\n<p style=\"padding-left: 30px\">15:06 C:\\&gt; $Error[0].InvocationInfo<\/p>\n<p style=\"padding-left: 30px\">MyCommand&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">BoundParameters&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : {}<\/p>\n<p style=\"padding-left: 30px\">UnboundArguments&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : {}<\/p>\n<p style=\"padding-left: 30px\">ScriptLineNumber&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 1<\/p>\n<p style=\"padding-left: 30px\">OffsetInLine&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 1<\/p>\n<p style=\"padding-left: 30px\">HistoryId&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : -1<\/p>\n<p style=\"padding-left: 30px\">ScriptName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">Line&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : [int8]::minvalue<\/p>\n<p style=\"padding-left: 30px\">PositionMessage&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : At line:1 char:1<\/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;&nbsp;&nbsp;&nbsp;&nbsp; + [int8]::minvalue<\/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;&nbsp;&nbsp;&nbsp;&nbsp; + ~~~~~~~~~~~~~~~~<\/p>\n<p style=\"padding-left: 30px\">PSScriptRoot&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">PSCommandPath&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">InvocationName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">PipelineLength&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"padding-left: 30px\">PipelinePosition&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"padding-left: 30px\">ExpectingInput&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : False<\/p>\n<p style=\"padding-left: 30px\">CommandOrigin&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Internal<\/p>\n<p style=\"padding-left: 30px\">DisplayScriptPosition :<\/p>\n<p>Still, this is pretty useful, and I would not have even come up with the idea if I had not attended the Windows PowerShell user group meeting the other night in Charlotte. In fact, I am thinking about putting this into a function called <strong>Remove-BadHistoryEntry<\/strong> and placing it in my profile. In fact, I just wrote the <a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/Remove-BadHistoryEntry-5a64e7c9\" target=\"_blank\">Remove-BadHistoryEntry function and uploaded it to the Scripting Guys Script Repository<\/a>. Check it out. It is cool.<\/p>\n<p>Join me tomorrow when I will talk about more way 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><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to remove all error commands from your Windows PowerShell command history. Microsoft Scripting Guy, Ed Wilson, is here. One of the great things about being involved with a Windows PowerShell user group is all the great people I meet and all the cool ideas that arise. It [&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":[68,3,4,45],"class_list":["post-4181","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-error-handling","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to remove all error commands from your Windows PowerShell command history. Microsoft Scripting Guy, Ed Wilson, is here. One of the great things about being involved with a Windows PowerShell user group is all the great people I meet and all the cool ideas that arise. It [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4181","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=4181"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4181\/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=4181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}