Summary: Use a custom Windows PowerShell function to simplify importing command history into the current environment.
Microsoft Scripting Guy Ed Wilson here, well it has been a fun week. I got to thaw out in Florida after my presentation to SQL Saturday in Tampa. I made some new friends on Twitter, Facebook, and Linked In. I had several really productive meetings in relation to the 2011 Scripting Games, and I even got to hang out with my old high school buddy and his family over the weekend. Yes, I do not think I could have scripted a better weekend.
But wait, there is more! I also wrote a cool function to simplify importing Windows PowerShell commands from a saved XML history file. If you use yesterday’s Export-History function, todays function will import it for you.
The two functions, Export-History and Import-History, both arose from two of my Hey, Scripting Guy! Blog posts I wrote this week. In my article about getting history and invoking history, I saw a need to simplify exporting Windows PowerShell command history. In the article about importing history, I thought it could be a bit confusing having to call the Import-Clixml Windows PowerShell cmdlet.
The complete Import-History function appears here.
Import-History.ps1
Function Import-History
{
<#
.Synopsis
Imports a saved XML file of history commands into current environment.
.Description
The Import-History function will simplify importing a saved XML file of
history commands into the current PowerShell environment.
.Example
Import-History -Path C:\fso\mycommands.xml
Imports history of commands saved in the mycommands.xml file
.Example
Import-History
This will prompt you for path to a saved xml file of history commands
.Example
Import-History c:\fso\mycommands.xml
Imports history of commands saved in the mycommands.xml file
.Parameter Path
The path to the saved XML file of history commands
.Inputs
[string]
.Outputs
None
.Notes
NAME: Import-XML
AUTHOR: Ed Wilson
LASTEDIT: 1/18/2011
KEYWORDS: Scripting Techniques, Getting Started
NOTES: You can use the Export-History function from HSG-1-22-2011 to export
the history commands from your PowerShell environment. You will need to add
this function to your environment by either dot-sourcing in the console, or
running in the ISE. You can also add this and the Export-History function
to your profile. For details see the Hey, Scripting Guy! Blog posts of
1/22/2011 and 1/23/2011.
.Link
Http://www.ScriptingGuys.com
#Requires -Version 2.0
#>
Param(
[Parameter(Mandatory=$true)]
[string]$Path)
Add-History -InputObject (Import-Clixml -path $path)
} #end function Import-History
Because I was a bit bored, I decided to add comment-based help to the Import-History function. The comment-based help actually takes up most of the space in the function. I discussed adding help for function parameters in the Hey, Scripting Guy! post, How Do I Add Help Information for Windows PowerShell Parameters. In the Automatically Add Comment-Based Help to Your PowerShell Scripts article, I talk about a function I wrote to add the help tags to a script. The comment-based help appears here.
<#
.Synopsis
Imports a saved XML file of history commands into current environment.
.Description
The Import-History function will simplify importing a saved XML file of
history commands into the current Windows PowerShell environment.
.Example
Import-History -Path C:\fso\mycommands.xml
Imports history of commands saved in the mycommands.xml file.
.Example
Import-History
This will prompt you for the path to a saved XML file of history commands.
.Example
Import-History c:\fso\mycommands.xml
Imports history of commands saved in the mycommands.xml file.
.Parameter Path
The path to the saved XML file of history commands.
.Inputs
[string]
.Outputs
None
.Notes
NAME: Import-XML
AUTHOR: Ed Wilson
LASTEDIT: 1/18/2011
KEYWORDS: scripting techniques, getting started
NOTES: You can use the Export-History function from HSG-1-22-2011 to export
the history commands from your Windows PowerShell environment. You will need to add
this function to your environment by either dot-sourcing in the console, or
running in the ISE. You can also add this and the Export-History function
to your profile. For details see the Hey, Scripting Guy! Blog articles of
1/22/2011 and 1/23/2011.
.Link
Http://www.ScriptingGuys.com
#Requires -Version 2.0
#>
The core portion of the Import-History function consists of a mandatory parameter and an Add-History command. This is shown here:
Param(
[Parameter(Mandatory=$true)]
[string]$Path)
Add-History -InputObject (Import-Clixml -path $path)
To use the Import-History function, you either dot-source the function, run it one time in your Windows PowerShell ISE, or add it to your profile or a module. After you have loaded it into your environment, type Import-History and specify a path to the history XML file you want to use. This is illustrated in the following image.
If you add the two functions to your profile or to a module, you may wish to create two aliases to simplify access to them. On my system, both IM and EM (for Import-Module and Export-Module, respectively) are available. I used Get-Alias (alias is gal) to see if they are in use. The output appears as errors (because the aliases are not available). I have drawn arrows to the applicable portion of the error message in the following image.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
0 comments