{"id":7491,"date":"2015-03-04T00:01:00","date_gmt":"2015-03-04T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/03\/04\/dont-script-edit-powershell-command-history\/"},"modified":"2019-02-18T10:30:24","modified_gmt":"2019-02-18T17:30:24","slug":"dont-script-edit-powershell-command-history","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/dont-script-edit-powershell-command-history\/","title":{"rendered":"Don&#8217;t Script: Edit PowerShell Command History"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about editing the Windows PowerShell command history.<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy! I liked your ideas in <a href=\"\/b\/heyscriptingguy\/archive\/2015\/03\/03\/avoid-scripting-use-powershell-command-history.aspx\" target=\"_blank\">Avoid Scripting: Use PowerShell Command History<\/a>. My problem is that I seldom (if ever) type commands perfectly the first time&mdash;often, not even the second time. This means that I end up with a lot of junk in my command history. I wish there was a way to clean up the command history so it would be more usable. Is there?<\/p>\n<p>&mdash;AP<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello AP,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. This afternoon, I am listening to a Canadian rock group and sipping some pomegranate tea. Of course, pomegranate is not really a tea, and a pomegranate tea is actually an herbal fruit kind of concoction. But hey&mdash;it is still nice and refreshing, and it doesn&rsquo;t have any caffeine in it. Along with chocolate biscotti, it makes a great way to work.<\/p>\n<p>AP, another great way to work is to use your Windows PowerShell command history. It is actually very flexible, and it is pretty easy to edit the history if you want to do so.<\/p>\n<p>Editing the Windows PowerShell command history is perhaps a misnomer. I cannot really edit the command history, but I can delete items from it and add items to it. I cannot edit an actual entry from the command history&mdash;well actually, I could, but I would need to export the history to a file, edit the file, and then import the history back in, which is a bit more work than I want to do. (If this was a scenario that I found myself doing on a regular basis, I could write a function to make it more doable.)<\/p>\n<p>How do I remove stuff from my Windows PowerShell command history? I use the <b>Clear-History<\/b> cmdlet. I will admit that when I first saw this cmdlet, I thought that it&#8230;well&#8230;cleared all of my history from Windows PowerShell. And it will do that, of course. Here is an example:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Clear-History<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt;<\/p>\n<p>I type the command, and nothing returns. If I want to see a bit more information, I can use the standard <b>&ndash;Verbose<\/b> Windows PowerShell parameter. This will tell me that the command is clearing my Windows PowerShell command history. This is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Clear-History -Verbose<\/p>\n<p style=\"margin-left:30px\">VERBOSE: Performing the operation &quot;Clear-History&quot; on target &quot;This command will clear all the entries from the session history.&quot;<\/p>\n<p>As shown here, I can also use the <b>&ndash;WhatIf<\/b> parameter if I am unsure that I want to remove all items:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Clear-History -WhatIf<\/p>\n<p style=\"margin-left:30px\">What if: Performing the operation &quot;Clear-History&quot; on target &quot;This command will clear all the entries from the session history.&quot;<\/p>\n<p>But the cmdlet is more flexible than simply emptying my Windows PowerShell history. I could remove specific items from my Windows PowerShell history. Before I remove a specific item from the Windows PowerShell command history, I like to look at the command. I can do this by using the <b>Get-History<\/b> cmdlet and specify a specific ID number. I then pipe the results to the <b>Format-List<\/b> cmdlet so I can see more information than the standard output. Here is the standard output:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Get-History 67<\/p>\n<p style=\"margin-left:30px\">&nbsp; Id CommandLine<\/p>\n<p style=\"margin-left:30px\">&nbsp; &#8212; &nbsp; &#8212;&#8212;&#8212;&#8211;<\/p>\n<p style=\"margin-left:30px\">&nbsp; 67 srv<\/p>\n<p>Here is the more detailed output:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Get-History 67 | fl *<\/p>\n<p style=\"margin-left:30px\">Id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 67<\/p>\n<p style=\"margin-left:30px\">CommandLine&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : srv<\/p>\n<p style=\"margin-left:30px\">ExecutionStatus&nbsp;&nbsp;&nbsp; : Completed<\/p>\n<p style=\"margin-left:30px\">StartExecutionTime : 3\/2\/2015 3:15:36 PM<\/p>\n<p style=\"margin-left:30px\">EndExecutionTime&nbsp;&nbsp; : 3\/2\/2015 3:15:36 PM<\/p>\n<p>Because a command completed (as did the previous command), it does not mean that it completed successfully. Here is what happens when I try to use the <b>Invoke-History<\/b> cmdlet (<b>r<\/b> is an alias) to execute the command:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; r 67<\/p>\n<p style=\"margin-left:30px\">srv<\/p>\n<p style=\"margin-left:30px\">srv : The term &#039;srv&#039; is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.<\/p>\n<p style=\"margin-left:30px\">At line:1 char:1<\/p>\n<p style=\"margin-left:30px\">+ srv<\/p>\n<p style=\"margin-left:30px\">+ ~~~<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; + CategoryInfo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : ObjectNotFound: (srv:String) [], CommandNotFoundExcep<span style=\"font-size:12px\">tion<\/span><\/p>\n<p>&nbsp;&nbsp;&nbsp; + FullyQualifiedErrorId : CommandNotFoundException<\/p>\n<h2>Finding a specific command<\/h2>\n<p>So I have a command that doesn&rsquo;t work in my Windows PowerShell command history. I can find all of these types of commands by using the <b>Get-History<\/b> command and piping the results to the <b>Where-Object<\/b> cmdlet. I already know that I have a property called <b>CommandLine<\/b> that contains my commands. Here is the command I use to find the commands:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Get-History | where commandline -eq &#039;srv&#039;<\/p>\n<p style=\"margin-left:30px\">&nbsp; Id CommandLine<\/p>\n<p style=\"margin-left:30px\">&nbsp; &#8212; &nbsp; &#8212;&#8212;&#8212;&#8211;<\/p>\n<p style=\"margin-left:30px\">&nbsp; 67 srv<\/p>\n<p style=\"margin-left:30px\">&nbsp; 78 srv<\/p>\n<p style=\"margin-left:30px\">&nbsp; 81 srv<\/p>\n<h2>Removing the bad commands from history<\/h2>\n<p>I thought I might need to pipe the results of my <b>Get-History<\/b> cmdlet to the <b>Clear-History<\/b> cmdet. But as it turns out, I don&rsquo;t need to do that. I can use <b>Clear-History<\/b> to search for and remove my bad command:<\/p>\n<p style=\"margin-left:30px\">Clear-History -CommandLine &#039;srv&#039;<\/p>\n<p>Sweet&hellip;no errors. Then I decide to use the Up arrow to recall my previous <b>Get-History<\/b> command. Bummer. I still have a couple of entries remaining:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Get-History | where commandline -eq &#039;srv&#039;<\/p>\n<p style=\"margin-left:30px\">&nbsp; Id CommandLine<\/p>\n<p style=\"margin-left:30px\">&nbsp; &#8212; &nbsp; &#8212;&#8212;&#8212;&#8211;<\/p>\n<p style=\"margin-left:30px\">&nbsp; 78 srv<\/p>\n<p style=\"margin-left:30px\">&nbsp; 81 srv<\/p>\n<p>I remember to use the count. I see that there are two instances of the bad command still in history, so I use the Up arrow and add the count. Here is my command:<\/p>\n<p style=\"margin-left:30px\">Clear-History -CommandLine &#039;srv&#039; -Count 2<\/p>\n<p>Now when I use the <b>Get-History<\/b> command, nothing appears.<\/p>\n<p>These commands and their associated output are shown in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-4-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-4-15-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>AP, that is all there is to editing the Windows PowerShell command history. Script Without Scripting Week will continue tomorrow when I will talk about using code snippets.<\/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><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about editing the Windows PowerShell command history. &nbsp;Hey, Scripting Guy! I liked your ideas in Avoid Scripting: Use PowerShell Command History. My problem is that I seldom (if ever) type commands perfectly the first time&mdash;often, not even the second time. This means that I end up with 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":[51,3,4,45],"class_list":["post-7491","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about editing the Windows PowerShell command history. &nbsp;Hey, Scripting Guy! I liked your ideas in Avoid Scripting: Use PowerShell Command History. My problem is that I seldom (if ever) type commands perfectly the first time&mdash;often, not even the second time. This means that I end up with a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7491","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=7491"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7491\/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=7491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=7491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=7491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}