Summary: Use the Export-History Windows PowerShell function to simplify saving command history.

Microsoft Scripting Guy Ed Wilson here. One of the things I was thinking about when I was writing the article this week about the History cmdlets was that I wish it were a bit easier to export my history. This gave me an idea: Why don’t I write a function to simplify exporting my command history? I decided it would be cool if I could type Export-History and a path to the XML file. In addition, I thought it is important to be able to cherry-pick which commands I want to include in the history file. After all, opening a new Windows PowerShell console and attempting to retype a whole bunch of commands perfectly can be a pain.

With these two design criteria in mind, I came up with the Export-History function shown here.

Export-History.ps1

Function Export-History

{

[CmdletBinding()]

Param(

[Parameter(mandatory=$false,

Position=0,

ValueFromPipeline=$True)]

$CommandInfo,

[Parameter(Mandatory=$true,

Position=1,

HelpMessage="Type Path for history file")]

$Path)

if(!$commandInfo)

{

Get-History | Export-Clixml -Path $path

}

Else

{ $input | Export-Clixml -Path $path }

} #end function Export-History

# Get-History (6,7) | Export-History -Path c:\fso\mycommands.xml

# Export-History -Path c:\fso\a.xml

The Get-History function begins by creating two parameters. The first parameter is the commandInfo parameter. It will accept a value from the pipeline, and is not mandatory. The second parameter is the path parameter, and it is mandatory. It tells the function where to save the XML file. This portion of the function is shown here.

Function Export-History

{

[CmdletBinding()]

Param(

[Parameter(mandatory=$false,

Position=0,

ValueFromPipeline=$True)]

$CommandInfo,

[Parameter(Mandatory=$true,

Position=1,

HelpMessage="Type Path for history file")]

$Path)

Now I need to add a little bit of logic. If the commandInfo parameter is not supplied, I want to export the entire history buffer from the current Windows PowerShell session. If, on the other hand, I call the Export-History function and take the trouble to select specific history commands, the function accepts the history information from the pipeline. This is the applicable portion of the function:

if(!$commandInfo)

{

Get-History | Export-Clixml -Path $path

}

Else

{ $input | Export-Clixml -Path $path }

} #end function Export-History

At the bottom of the Export-History.ps1 file, I have two examples of calling the function. One illustrates calling the function with no commandInfo, and the other pipelines the commandInfo to the function. These two commands are shown here:

# Get-History (6,7) | Export-History -Path c:\fso\mycommands.xml

# Export-History -Path c:\fso\a.xml

So, how do you actually use the Export-History function? For one thing, I can dot-source it into my Windows PowerShell console session. This is shown in the following image and it allows me to then run the function like any other Windows PowerShell cmdlet. I type Export-History and am prompted for the path to save the command history XML file. This is because I marked the path parameter as mandatory.

Image of dot-sourcing the function

After the XML file has been exported, I can use Internet Explorer to review the XML file to see if it contains the commands I wanted to export. The command history XML file appears in the following image. Just for fun, I have drawn arrows to the two commands in which I am interested.

Image of viewing the XML file in Internet Explorer

You can open the script in the Windows PowerShell ISE, and edit one of the command lines. After this is accomplished, you can run the script. This is shown in the following image.

Image of the function in the PowerShell ISE

You can leave the two commands commented out, and simply run the Export-History function in the Windows PowerShell ISE console. Then, in the immediate window (the bottom pane), you can type the command as you would in the Windows PowerShell console, or as the command might appear in the script itself. What you have accomplished is to load the function onto the function drive (the same as dot-sourcing in the Windows PowerShell console). You can then refer to the function whenever you wish. This technique is illustrated in the following image (I have drawn an arrow to point to the command pane).

Image of loading the function onto the function drive

You could also include the Export-History function in your Windows PowerShell profile. This is the option I like the best. I have written several Hey, Scripting Guy! Blog posts that talk about using the Windows PowerShell profile. Refer to them for ideas about incorporating the Export-History function. Join me tomorrow as I develop an Import-History function.

I invite you to follow me on Twitter or Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum.

 

Ed Wilson, Microsoft Scripting Guy