Click to Rate and Give Feedback
TechNet
TechNet Library
Scripting
Windows PowerShell
 Add-History
Add-History

Appends entries to the session history.

Syntax

add-history [[-inputObject] <PSObject[]>] [-passthru] [<CommonParameters>]

Detailed 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 Get-History to get the commands and pass them to Add-History, or 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.

Parameters

-inputObject <PSObject[]>

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

Required?

false

Position?

1

Default Value

none

Accept Pipeline Input?

True <ByValue>

Accept Wildcard Characters?

false

-passThru <SwitchParameter>

Passes an object for each history entry to the pipeline. By default, this cmdlet does not generate any output.

Required?

false

Position?

named

Default Value

False

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

<CommonParameters>

This cmdlet supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutBuffer and -OutVariable. For more information, see about_CommonParameters.

Input and Return Types

The input type is the type of the objects that you can pipe to the cmdlet. The return type is the type of the objects that the cmdlet emits.

Input Type

HistoryInfo object (Microsoft.PowerShell.Commands.HistoryInfo). You can pipe a HistoryInfo object from Get-History, Import-Clixml, or Import-Csv to Add-History.

Return Type

None. Unless the Passthru parameter is used. If Passthru is used, a HistoryInfo object that represents the added history items is passed down the pipeline.

Notes

  • To specify the commands to add to the history, use the InputObject parameter. The Add-History command accepts only 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 the 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 object to Add-History through 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 headings, or any fields in the file.
  • The session history is a list of the commands entered during the session, along with the ID, which represents the order of execution, the status, and start and end times of the command. As you enter each command, Windows PowerShell adds it to the history so that you can reuse it. For more information about the session history, see about_History.
  • You cannot clear the session history except by ending the Windows PowerShell session.
  • To edit the session history, export the session to a CSV or XML file, edit the file, import the file, and use Add-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

These commands add the commands typed in one Windows PowerShell session to the history of a different Windows PowerShell session. 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 that represent 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}

This command imports commands in the History.xml file, adds them to the current session history, and then executes the commands in the combined history. The first command uses the Import-Clixml cmdlet to import a command history that was exported to the History.xml file. The pipeline operator (|) passes the commands to the Add-History parameter, 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 curly braces ({})) because 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

This command adds the first five commands in the history to the end of the history list. It uses the Get-History cmdlet 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 PowerShell 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

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 $a variable. The second command uses the Add-History cmdlet to add the commands from History.csv to the current session history. It uses 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)

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

Example 6

C:\PS> $a = get-history

C:\PS> $a

C:\PS> $b = $a[0..2 + 4..5]

C:\PS> $b

This example shows how to delete items from the command history. Because the command history is an array of history objects, you can use array math to delete history items.

The first command uses the Get-History cmdlet to get the current command history. It saves the history in the $a variable. The second command displays the history in the $a variable.

The third command creates a new array and stores it in the $b variable. The value of $b is the value of $a, except for the fourth item ($a[3], in a zero-based array). The command uses the range operator (..) to indicate the items between 0 and 2, and 4 and 5.

For more information, see about_Array

and about_History.

The following sample output shows the results of these commands.

C:\PS> $a = get-history

C:\PS> $a

  Id CommandLine
  -- -----------
   1 get-help invoke-expression
   2 get-command invoke-expression -syntax
   3 get-help invoke-item -example
   4 get-process
   5 $p = get-process
   6 $p.count

C:\PS> $b = $a[0..2 + 4..5]
C:\PS> $b

  Id CommandLine
  -- -----------
   1 get-help invoke-expression
   2 get-command invoke-expression -syntax
   3 get-help invoke-item -example
   5 $p = get-process
   6 $p.count

See Also



Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker