Add-History | Taking on PowerShell one cmdlet at a time | Weekly Blog

Share this post:

This is a part of an on-going blog series written by Adam Gordon. Each week, Adam will walk you through a PowerShell command, showing you when and how to use each one. This week, Adam covers Add-History. 

When to use Add-History?

The Add-History cmdlet adds entries to the end of the session history, that is, the list of commands entered during the current session.

The session history is a list of the commands entered during the session. The session history represents the order of execution, the status, and the start and end times of the command. As you enter each command, PowerShell adds it to the history so that you can reuse it.

The session history is managed separately from the history maintained by the PSReadLine module. Both histories are available in sessions where PSReadLine is loaded.

This cmdlet only works with the session history.

You can use the Get-History cmdlet to get the commands and pass them to Add-History, or you can export the commands to a CSV or XML file, then import the commands, and pass the imported file to Add-History. You can use this cmdlet to add specific commands to the history or to create a single history file that includes commands from more than one session.

What version of PowerShell am I using for this blog?

Get the PowerShell Version from your machine:

$PSVersionTable

This command shows you the PowerShell version information on your machine.

How to use Add-History?

Add commands to the history of a different session: 

1st command: Get-History | Export-Csv c:\PShellTest\history.csv -IncludeTypeInformation

2nd command: Import-Csv c:\PShellTest\history.csv | Add-History

The first command gets objects representing the commands in the history and exports them to the History.csv file.

The second command is typed at the command line of a different session.

It uses the Import-Csv cmdlet to import the objects in the History.csv file.

The pipeline operator (|) passes the objects to the Add-History cmdlet, which adds the objects representing the commands in the History.csv file to the current session history.

 

 

 

 

 

Import and run commands:

1st command: Get-History | Export-Clixml c:\PShellTest\history2.xml

2nd command: Import-Clixml c:\PShellTest\history2.xml | Add-History -PassThru | ForEach-Object -Process {Invoke-History}

The first command gets objects representing the commands in the history and exports them to the history2.xml file.

The second command uses the Import-Clixml cmdlet to import a command history that was exported to the history2.xml file. The pipeline operator passes the commands to the Add-History cmdlet, which adds the commands to the current session history.

The PassThru parameter passes the objects representing the added commands down the pipeline.

The command then uses the ForEach-Object cmdlet to apply the Invoke-History command to each of the commands in the combined history.

The Invoke-History command is formatted as a script block, enclosed in braces ({}), as required by the –Process parameter of the ForEach-Object cmdlet.

 

 

Take a look at last week’s command: Test-Connection.

Need PowerShell training? Check out ITProTV’s PowerShell online IT training courses.

Leave a Reply

Your email address will not be published. Required fields are marked *