{"id":6371,"date":"2015-04-29T00:01:00","date_gmt":"2015-04-29T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/04\/29\/powershell-tips-and-tricks-extended-history\/"},"modified":"2019-02-18T10:29:43","modified_gmt":"2019-02-18T17:29:43","slug":"powershell-tips-and-tricks-extended-history","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powershell-tips-and-tricks-extended-history\/","title":{"rendered":"PowerShell Tips and Tricks: Extended History"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about a module that provides extended command history.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. One of the things that came up during the Charlotte Windows PowerShell User Group meeting was the HistoryPx module that was written by Windows PowerShell MVP, Kirk Munro. This module adds additional history information to the Windows PowerShell console. For example, he adds information about the duration of a command, the output of the command, and errors generated by the command.<\/p>\n<h2>Installing&nbsp; HistoryPx<\/h2>\n<p>Installing the HistoryPx module is really easy because it is hosted on GitHub, and therefore I can use PSGet to download and install the module.<\/p>\n<p style=\"margin-left:30px\"><b>Note&nbsp;<\/b> PSGet is a community-developed module that streamlines the installation of modules. It is available from <a href=\"http:\/\/psget.net\/\" target=\"_blank\">PSGet.net<\/a>. It provides cmdlets such as <b>Install-Module<\/b> and <b>Update-Module<\/b>.<\/p>\n<p>To install the HistoryPx module, I also need to ensure that I have the SnippetPX module. Luckily, I can install both modules by using the following command, which will install the modules for all users:<\/p>\n<p style=\"margin-left:30px\">Install-Module HIstoryPx, SnippetPx<\/p>\n<p style=\"margin-left:30px\"><b>Note<\/b>&nbsp;&nbsp;Running this command requires elevated permissions.<\/p>\n<p>I can also install the modules for me only. If I do not want to be prompted with the EULA, then I use the <b>&ndash;AcceptEula<\/b> switch. This command is shown here:<\/p>\n<p style=\"margin-left:30px\">Install-Module HistoryPx, SnippetPx&nbsp; -Scope CurrentUser -AcceptEula<\/p>\n<h2>Using the extended history module<\/h2>\n<p>After I have installed the HistoryPx module and the SnippetPx module, I can import the HistoryPx module by using the <b>Import-Module<\/b> cmdlet:<\/p>\n<p style=\"margin-left:30px\">Import-Module HistoryPx<\/p>\n<p>I can now use the standard <b>Get-History<\/b> cmdlet to retrieve my command history. This is because the HistoryPx module creates proxy functions (that is, it extends the standard <b>Get-History<\/b> cmdlet).<\/p>\n<p style=\"margin-left:30px\"><b>Note&nbsp;<\/b> There are several posts on the Hey, Scripting Guy! Blog about proxy functions. A good starting point is one by Windows PowerShell MVP, Shay Levy, called <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/proxy-functions-spice-up-your-powershell-core-cmdlets\/\" target=\"_blank\">Proxy Functions: Spice Up Your PowerShell Core cmdlets<\/a>.<\/p>\n<p>Here is the output from the <b>Get-History<\/b> cmdlet now (<b>h<\/b> is an alias for <b>Get-History<\/b>):<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-4-29-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-4-29-15-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>One of the cool things about the module is that it automatically captures output in a variable, <b>$__ <\/b>,<b> <\/b>(it has two underscores). This means that when I am working interactively in the Windows PowerShell console, I do not need to use an intermediate variable. I can use the <b>$__<\/b> variable and filter it directly. This technique is shown here, where I look for commands that contain the word <b>Install<\/b>:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $__.where{$_.commandline -match &#039;install&#039;<\/p>\n<p style=\"margin-left:30px\">&nbsp;Id &nbsp; CommandLine &nbsp; &nbsp; &nbsp; Duration &nbsp; &nbsp; Success &nbsp; #Errors &nbsp; Output<\/p>\n<p style=\"margin-left:30px\">&nbsp; &#8212; &nbsp; &nbsp; &nbsp;&#8212;&#8212;&#8212;&#8211; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;- &nbsp; &nbsp; &nbsp; &#8212;&#8212;- &nbsp; &nbsp; &#8212;&#8212;<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; 2 Install-Module HistoryPx, S&#8230; 00:00:02.515&nbsp;&nbsp; True<\/p>\n<p style=\"margin-left:30px\">&nbsp; 10 $__.where({$psitem.commandl&#8230; 00:00:00.015&nbsp;&nbsp; True&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {}<\/p>\n<p style=\"margin-left:30px\">&nbsp; 12 $__.where{$_.commandline -m&#8230; 00:00:00.000&nbsp;&nbsp; True&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {}<\/p>\n<p>I see the command I was interested in is command number two. So I can now use <b>Get-History<\/b> to look directly at the command history for that command. This is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; h 2<\/p>\n<p style=\"margin-left:30px\">&nbsp; Id &nbsp; CommandLine &nbsp; &nbsp; &nbsp; Duration &nbsp; &nbsp; Success &nbsp; #Errors &nbsp; Output<\/p>\n<p style=\"margin-left:30px\">&nbsp; &#8212; &nbsp; &nbsp; &nbsp;&#8212;&#8212;&#8212;&#8211; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;- &nbsp; &nbsp; &nbsp; &#8212;&#8212;- &nbsp; &nbsp; &#8212;&#8212;<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; 2 Install-Module HistoryPx, S&#8230; 00:00:02.515&nbsp;&nbsp; True<\/p>\n<p>There is a lot more I can do with this module, and Kirk has documented it well in his GitHub project <a href=\"https:\/\/github.com\/KirkMunro\/HistoryPx\" target=\"_blank\">HistoryPx<\/a>. You may want to install the modules, play around with them, and see if they help you work more effectively.<\/p>\n<p>Windows PowerShell Tips and Tricks Week continues tomorrow when I will talk about more cool 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><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about a module that provides extended command history. Microsoft Scripting Guy, Ed Wilson, is here. One of the things that came up during the Charlotte Windows PowerShell User Group meeting was the HistoryPx module that was written by Windows PowerShell MVP, Kirk Munro. This module adds additional history [&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":[3,4,578,45],"class_list":["post-6371","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-tips-and-tricks","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about a module that provides extended command history. Microsoft Scripting Guy, Ed Wilson, is here. One of the things that came up during the Charlotte Windows PowerShell User Group meeting was the HistoryPx module that was written by Windows PowerShell MVP, Kirk Munro. This module adds additional history [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/6371","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=6371"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/6371\/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=6371"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=6371"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=6371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}