PowerShell Tips and Tricks: Extended History


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 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.

Installing  HistoryPx

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.

Note  PSGet is a community-developed module that streamlines the installation of modules. It is available from PSGet.net. It provides cmdlets such as Install-Module and Update-Module.

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:

Install-Module HIstoryPx, SnippetPx

Note  Running this command requires elevated permissions.

I can also install the modules for me only. If I do not want to be prompted with the EULA, then I use the –AcceptEula switch. This command is shown here:

Install-Module HistoryPx, SnippetPx  -Scope CurrentUser -AcceptEula

Using the extended history module

After I have installed the HistoryPx module and the SnippetPx module, I can import the HistoryPx module by using the Import-Module cmdlet:

Import-Module HistoryPx

I can now use the standard Get-History cmdlet to retrieve my command history. This is because the HistoryPx module creates proxy functions (that is, it extends the standard Get-History cmdlet).

Note  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 Proxy Functions: Spice Up Your PowerShell Core cmdlets.

Here is the output from the Get-History cmdlet now (h is an alias for Get-History):

Image of command output

One of the cool things about the module is that it automatically captures output in a variable, $__ , (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 $__ variable and filter it directly. This technique is shown here, where I look for commands that contain the word Install:

PS C:\> $__.where{$_.commandline -match 'install'

 Id   CommandLine       Duration     Success   #Errors   Output

  —      ———–               ——–       ——-       ——-     ——

   2 Install-Module HistoryPx, S… 00:00:02.515   True

  10 $__.where({$psitem.commandl… 00:00:00.015   True    0       {}

  12 $__.where{$_.commandline -m… 00:00:00.000   True    0       {}

I see the command I was interested in is command number two. So I can now use Get-History to look directly at the command history for that command. This is shown here:

PS C:\> h 2

  Id   CommandLine       Duration     Success   #Errors   Output

  —      ———–               ——–       ——-       ——-     ——

   2 Install-Module HistoryPx, S… 00:00:02.515   True

There is a lot more I can do with this module, and Kirk has documented it well in his GitHub project HistoryPx. You may want to install the modules, play around with them, and see if they help you work more effectively.

Windows PowerShell Tips and Tricks Week continues tomorrow when I will talk about more cool stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Comments (4)

  1. Amit says:

    My Powershell script is behaving differennt when i am running from Powershell ISC and through the SQL Agent job when both the place version on powershell is same.

  2. Jason Poad says:

    Ed, it looks like you may be using a different version of Install-Module, and not the version from PsGet. The xml manifest that PsGet’s version downloads doesn’t even include the SnippetPx or HistoryPx modules. And PsGet’s version doesn’t have the -AcceptEula
    switch.

  3. Florian Frank says:

    Ed, thank you for this article. But I got the same problem as Jason. I can’t install SnippetPx or HistoryPx through Install-Module (‘Accept-Eula’ for example is also missing).

  4. Florian Frank says:

    I found another to Install this module via powershell by using "Install-Module -ModuleUrl" with the URL of the ZIP on GitHub 😉

Skip to main content