{"id":15821,"date":"2011-01-23T00:01:00","date_gmt":"2011-01-23T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/01\/23\/simplify-importing-powershell-command-history\/"},"modified":"2011-01-23T00:01:00","modified_gmt":"2011-01-23T00:01:00","slug":"simplify-importing-powershell-command-history","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/simplify-importing-powershell-command-history\/","title":{"rendered":"Simplify Importing PowerShell Command History"},"content":{"rendered":"<p><b><\/b><\/p>\n<p><b>Summary<\/b>: Use a custom Windows PowerShell function to simplify importing command history into the current environment.<\/p>\n<p>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. <\/p>\n<p>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 <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/01\/22\/use-the-export-history-powershell-function-to-save-commands.aspx\">yesterday\u2019s Export-History function<\/a>, todays function will import it for you. <\/p>\n<p>The two functions, <b>Export-History<\/b> and <b>Import-History<\/b>, both arose from two of my Hey, Scripting Guy! Blog posts I wrote this week. In my <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/01\/18\/use-powershell-history-to-speed-repetitive-commands.aspx\">article about getting history and invoking history<\/a>, I saw a need to simplify exporting Windows PowerShell command history. In the <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/01\/19\/use-the-powershell-add-history-cmdlet-to-customize-the-console.aspx\">article about importing history<\/a>, I thought it could be a bit confusing having to call the <b>Import-Clixml<\/b> Windows PowerShell cmdlet. <\/p>\n<p>The complete <b>Import-History<\/b> function appears here.<\/p>\n<blockquote>\n<p><strong>Import-History.ps1<\/strong><\/p>\n<p>Function Import-History<\/p>\n<p>{<\/p>\n<p>&lt;#<\/p>\n<p>.Synopsis<\/p>\n<p>Imports a saved XML file of history commands into current environment. <\/p>\n<p>.Description<\/p>\n<p>The Import-History function will simplify importing a saved XML file of<\/p>\n<p>history commands into the current PowerShell environment. <\/p>\n<p>.Example<\/p>\n<p>Import-History -Path C:\\fso\\mycommands.xml<\/p>\n<p>Imports history of commands saved in the mycommands.xml file<\/p>\n<p>.Example<\/p>\n<p>Import-History<\/p>\n<p>This will prompt you for path to a saved xml file of history commands<\/p>\n<p>.Example<\/p>\n<p>Import-History c:\\fso\\mycommands.xml<\/p>\n<p>Imports history of commands saved in the mycommands.xml file<\/p>\n<p>.Parameter Path<\/p>\n<p>The path to the saved XML file of history commands<\/p>\n<p>.Inputs<\/p>\n<p>[string]<\/p>\n<p>.Outputs<\/p>\n<p>None<\/p>\n<p>.Notes<\/p>\n<p>NAME: Import-XML<\/p>\n<p>AUTHOR: Ed Wilson<\/p>\n<p>LASTEDIT: 1\/18\/2011<\/p>\n<p>KEYWORDS: Scripting Techniques, Getting Started<\/p>\n<p>NOTES: You can use the Export-History function from HSG-1-22-2011 to export <\/p>\n<p>the history commands from your PowerShell environment. You will need to add<\/p>\n<p>this function to your environment by either dot-sourcing in the console, or<\/p>\n<p>running in the ISE. You can also add this and the Export-History function<\/p>\n<p>to your profile. For details see the Hey, Scripting Guy! Blog posts of <\/p>\n<p>1\/22\/2011 and 1\/23\/2011.<\/p>\n<p>.Link<\/p>\n<p>Http:\/\/www.ScriptingGuys.com<\/p>\n<p>#Requires -Version 2.0<\/p>\n<p>#&gt;<\/p>\n<p>Param(<\/p>\n<p>[Parameter(Mandatory=$true)]<\/p>\n<p>[string]$Path)<\/p>\n<p>Add-History -InputObject (Import-Clixml -path $path)<\/p>\n<p>} #end function Import-History<\/p>\n<\/blockquote>\n<p>Because I was a bit bored, I decided to add comment-based help to the <b>Import-History<\/b> 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, <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/01\/07\/hey-scripting-guy-january-7-2010.aspx\">How Do I Add Help Information for Windows PowerShell Parameters<\/a>. In the <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/09\/11\/automatically-add-comment-based-help-to-your-powershell-scripts.aspx\">Automatically Add Comment-Based Help to Your PowerShell Scripts<\/a> article, I talk about a function I wrote to add the help tags to a script. The comment-based help appears here.<\/p>\n<blockquote>\n<p>&lt;#<\/p>\n<p>.Synopsis<\/p>\n<p>Imports a saved XML file of history commands into current environment. <\/p>\n<p>.Description<\/p>\n<p>The Import-History function will simplify importing a saved XML file of<\/p>\n<p>history commands into the current Windows PowerShell environment. <\/p>\n<p>.Example<\/p>\n<p>Import-History -Path C:\\fso\\mycommands.xml<\/p>\n<p>Imports history of commands saved in the mycommands.xml file.<\/p>\n<p>.Example<\/p>\n<p>Import-History<\/p>\n<p>This will prompt you for the path to a saved XML file of history commands.<\/p>\n<p>.Example<\/p>\n<p>Import-History c:\\fso\\mycommands.xml<\/p>\n<p>Imports history of commands saved in the mycommands.xml file.<\/p>\n<p>.Parameter Path<\/p>\n<p>The path to the saved XML file of history commands.<\/p>\n<p>.Inputs<\/p>\n<p>[string]<\/p>\n<p>.Outputs<\/p>\n<p>None<\/p>\n<p>.Notes<\/p>\n<p>NAME: Import-XML<\/p>\n<p>AUTHOR: Ed Wilson<\/p>\n<p>LASTEDIT: 1\/18\/2011<\/p>\n<p>KEYWORDS: scripting techniques, getting started<\/p>\n<p>NOTES: You can use the Export-History function from HSG-1-22-2011 to export <\/p>\n<p>the history commands from your Windows PowerShell environment. You will need to add<\/p>\n<p>this function to your environment by either dot-sourcing in the console, or<\/p>\n<p>running in the ISE. You can also add this and the Export-History function<\/p>\n<p>to your profile. For details see the Hey, Scripting Guy! Blog articles of <\/p>\n<p>1\/22\/2011 and 1\/23\/2011.<\/p>\n<p>.Link<\/p>\n<p>Http:\/\/www.ScriptingGuys.com<\/p>\n<p>#Requires -Version 2.0<\/p>\n<p>#&gt;<\/p>\n<\/blockquote>\n<p>The core portion of the <b>Import-History<\/b> function consists of a mandatory parameter and an <b>Add-History<\/b> command. This is shown here:<\/p>\n<blockquote>\n<p>Param(<\/p>\n<p>[Parameter(Mandatory=$true)]<\/p>\n<p>[string]$Path)<\/p>\n<p>Add-History -InputObject (Import-Clixml -path $path)<\/p>\n<\/blockquote>\n<p>To use the <b>Import-History<\/b> 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 <font face=\"Courier New\">Import-History<\/font> and specify a path to the history XML file you want to use. This is illustrated in the following image.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/7608.wes-1-23-11-1_037D6748.jpg\"><img decoding=\"async\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" title=\"Image of using Import-History function\" border=\"0\" alt=\"Image of using Import-History function\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/0434.wes-1-23-11-1_thumb_1F5E7673.jpg\" width=\"604\" height=\"472\" \/><\/a><\/p>\n<p>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 <b>IM<\/b> and <b>EM<\/b> (for <b>Import-Module<\/b> and <b>Export-Module<\/b>, respectively) are available. I used <b>Get-Alias<\/b> (alias is <b>gal<\/b>) to see if they are\nin 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.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/0830.wes-1-23-11-2_596D4352.jpg\"><img decoding=\"async\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" title=\"Image of error message saying aliases not available\" border=\"0\" alt=\"Image of error message saying aliases not available\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/6281.wes-1-23-11-2_thumb_1B90ABD6.jpg\" width=\"604\" height=\"169\" \/><\/a><\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p>&#160;<\/p>\n<p><b><\/b><\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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. [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,3,4,61,45],"class_list":["post-15821","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>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. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15821","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=15821"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15821\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=15821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=15821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=15821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}