Share this post:This blog post is part of an ongoing series by Adam Gordon. Adam will guide you through a PowerShell command each week, explaining when and how to use them. This week Adam will be covering Add-History.
When should you use Add-History
The Add-History cmdlet adds entries at the end of session history. This is the list of commands that were entered during the current session.
The session history contains a list of commands that were entered during the session. The session history records the order of execution, status, and start and end times for each command. PowerShell adds each command to the history as you type it, so you can reuse them.
The session history is separate from the history maintained in the PSReadLine module. Both histories are available in sessions that PSReadLine is loaded.
This cmdlet works only with session history
To get the commands and then pass them to Add History, you can either use the Get-History cmdlet or export them to a CSV/XML file. Then import the commands and send the imported file to Add History. This cmdlet can be used to add commands to the history, or to create a single history file with commands from multiple sessions.
What version of PowerShell should I use for this blog?
Get the PowerShell Version for your machine
This command displays the PowerShell version information for your machine.
How to use Add History?
Add commands to the history for 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 retrieves objects representing commands in the history and then exports them to the History.csv format.
The second command is entered at the command line for a different session.
It uses the Import Csv cmdlet for importing objects from the History.csv file.
) 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.
Run and import 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 retrieves objects representing commands in the history and then exports them to history2.xml.
The second command uses Import-Clixml to import a command log that was previously exported to history2.xml. The pipeline operator sends the commands to Add-History cmdlet. This adds the commands into the current session history.
The PassThru parameter sends the objects that represent the additional commands down the pipeline.
The command then uses ForEach-Object to apply the Invoke History command to each command in the combined history.
Invoke-History is formatted as a scriptblock, enclosed in braces (), according to the -Process parameter for the ForEachObject cmdlet.
Take a look at the command last week: Test-Connection.
Do you need PowerShell training? ITProTV offers PowerShell online IT training courses.