powershellscripts.com

Tutorials  PowerShell Cmdlet Help for Add-History



NAME
Add-History

SYNOPSIS
Appends entries to the session history.

SYNTAX
Add-History [[-InputObject] ] [-PassThru] []


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

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 o
ne session.


PARAMETERS
-InputObject
Adds the specified HistoryInfo object to the session history. You can use this parameter to submit a HistoryInf
o object from Get-History, Import-Clixml, or Import-Csv to Add-History.

Required? false
Position? 1
Default value
Accept pipeline input? true (ByValue)
Accept wildcard characters? false

-PassThru []
Returns a history object for each history entry. By default, this cmdlet does not generate any output.

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false


This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer and OutVariable. For more information, type,
"get-help about_commonparameters".

INPUTS
Microsoft.PowerShell.Commands.HistoryInfo
You can pipe a HistoryInfo object to Add-History.


OUTPUTS
None or Microsoft.PowerShell.Commands.HistoryInfo
When you use the PassThru parameter, Add-History generates a HistoryInfo object. Otherwise, this cmdlet does no
t generate any output.


NOTES


The session history is a list of the commands entered during the session along with the ID. The session history
represents the order of execution, the status, and the start and end times of the command. As you enter each c
ommand, Windows PowerShell adds it to the history so that you can reuse it. For more information about the ses
sion history, see about_History.

To specify the commands to add to the history, use the InputObject parameter. The Add-History command accepts o
nly HistoryInfo objects, such as those generated for each command by Get-History. You cannot pass it a path and
file name or a list of commands.

You can use the -InputObject parameter to pass a file of HistoryInfo objects to Add-History. To do so, export t
he results of a Get-History command to a file by using Export-Csv or Export-Clixml and then import the file by
using Import-Csv or Import-Clixml. You can then pass the file of imported HistoryInfo objects to Add-History th
rough a pipeline or in a variable. For more information, see the examples.

The file of HistoryInfo objects that you pass to Add-History must include the type information, column headings
, and all of the properties of the HistoryInfo objects. If you intend to pass the objects back to Add-History,
do not use the NoTypeInformation parameter of Export-Csv and do not delete the type information, column heading
s, or any fields in the file.

To edit the session history, export the session to a CSV or XML file, edit the file, import the file, and use A
dd-History to append it to the current session history.


-------------------------- EXAMPLE 1 --------------------------

C:\PS>get-history | export-csv c:\testing\history.csv

C:\PS>import-csv history.csv | add-history


Description
-----------
These commands add the commands typed in one Windows PowerShell session to the history of a different Windows Power
Shell session. The first command gets objects representing the commands in the history and exports them to the Hist
ory.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.





-------------------------- EXAMPLE 2 --------------------------

C:\PS>import-clixml c:\temp\history.xml | add-history -passthru | foreach-object {invoke-history}


Description
-----------
This command imports commands from the History.xml file, adds them to the current session history, and then execute
s the commands in the combined history.

The first command uses the Import-Clixml command to import a command history that was exported to the History.xml f
ile. The pipeline operator (|) passes the commands to the Add-History parameter, which adds the commands to the cur
rent 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, which is enclosed in braces ({}) becau
se ForEach-Object requires a script block even when there is only one command to apply.





-------------------------- EXAMPLE 3 --------------------------

C:\PS>get-history -id 5 -count 5 | add-history


Description
-----------
This command adds the first five commands in the history to the end of the history list. It uses the Get-History cm
dlet to get the five commands ending in command 5. The pipeline operator (|) passes them to the Add-History cmdlet,
which appends them to the current history. The Add-History command does not include any parameters, but Windows Po
werShell associates the objects passed through the pipeline with the InputObject parameter.





-------------------------- EXAMPLE 4 --------------------------

C:\PS>$a = import-csv c:\testing\history.csv

C:\PS>add-history -inputobject $a -passthru


Description
-----------
These commands add the commands in the History.csv file to the current session history. The first command uses the
Import-Csv cmdlet to import the commands in the History.csv file and store its contents in the variable $a. The sec
ond command uses the Add-History cmdlet to add the commands from History.csv to the current session history. It use
s the InputObject parameter to specify the $a variable and the PassThru parameter to generate an object to display
at the command line. Without the PassThru parameter, Add-History does not generate any output to display.





-------------------------- EXAMPLE 5 --------------------------

C:\PS>add-history -inputobject (import-clixml c:\temp\history01.xml)


Description
-----------
This command adds the commands in the History01.xml file to the current session history. It uses the InputObject pa
rameter to pass the results of the command in parentheses to Add-History. The command in parentheses, which is exec
uted first, imports the History01.xml file into Windows PowerShell. Add-History then adds the commands in the file
to the session history.






RELATED LINKS
Online version: http://go.microsoft.com/fwlink/?LinkID=113279
about_History
Get-History
Invoke-History
Clear-History