Get-History

History is important and those that don’t know it are doomed to repeat it.

OK. We aren’t talking about that kind of history. In this Tip of the Week we will be referring to PowerShell session command history. PowerShel

While building scripts or working in a particular environment, we may want to review a particular cmdlet or one-liner we previously used. One method for cycling through these cmdlets is to use the up arrow (or down arrow) to have PowerShell display these past commands.

Get-History

But what if we need to see more, perhaps a series of cmdlets that led to a current result?
[sourcecode language=”powershell”]
Get-Command *-History
[/sourcecode]

As we can see, there is a series of cmdlets that can help us explore the cmdlet history in PowerShell. First, let’s see what we can do with Get-History:
[sourcecode language=”powershell”]
Get-Help Get-History -Examples
[/sourcecode]

If we review the available parameters we see we only have ID and Count. Let’s see what we can do with these parameters.

For example, if we were reviewing an Exchange environment and exporting certain information to files, we might see something like this:

In another example, we are reviewing Active Directory and documenting settings, we might see something like this:

Now, if you look at lines 12 and 17, we see there is a ‘…’ at the end of the line. This means that there are hidden details. How can we see what is hidden?
[sourcecode language=”powershell”]
Get-History -Id 17 | Fl
[/sourcecode]

Same for 12:

Now if we wanted to flush the cache of what was run in our current PowerShell window, we can run the Clear-History cmdlet. Then we can run Get-History to verify no history is left:

Notice that even after we cleared our PowerShell history, the ID count for the commands executed is still incrementing.

Add-History

Add-History may see like an odd cmdlet to use in PowerShell, but it’s intent is to help an operator keep track of cmdlets that have been run in one or more shell windows. We can use the cmdlet to possibly add the history from other PowerShell sessions to consolidate what has been executed. Combined this with a Get-History one-liner we can document all items that were run by exporting the list to an external file for later use, for auditing or just documentation purposes.

Examples from Add-History:

Invoke-History

If we want, once we have the information on a particular command, we can re-execute the cmdlet with the Invoke-history cmdlet. See the following examples as guidance:

Using the previous list of cmdlets for Active Directory, if we wanted to re-run #17 or #12, we simply need to execute this:

However, if we’ve already cleared the information from PowerShell history, we will receive the above error. If we have not cleared the Exchange examples we can try something like ID 37:
[sourcecode language=”powershell”]
Invoke-History -Id 37
[/sourcecode]
We would see something like this:

Summary

The *History set of cmdlets provides a good insight into what was run in the PowerShell session that we are currently in. The cmdlet set can easily supplement your current workflows to provide a way to keep track of what is being used to possibly checking syntax or re-executing a one-liner for some other workflow. Be careful with Clear-History as you may lose crucial information by using it.

Related Post