{"id":15831,"date":"2011-01-22T00:01:00","date_gmt":"2011-01-22T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/01\/22\/use-the-export-history-powershell-function-to-save-commands\/"},"modified":"2011-01-22T00:01:00","modified_gmt":"2011-01-22T00:01:00","slug":"use-the-export-history-powershell-function-to-save-commands","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-the-export-history-powershell-function-to-save-commands\/","title":{"rendered":"Use the Export-History PowerShell Function to Save Commands"},"content":{"rendered":"<p>&#160;<\/p>\n<p><b>Summary<\/b>: Use the Export-History Windows PowerShell function to simplify saving command history.<\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. One of the things I was thinking about when I was writing <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/01\/18\/use-powershell-history-to-speed-repetitive-commands.aspx\">the article this week about the History cmdlets<\/a> was that I wish it were a bit easier to export my history. This gave me an idea: Why don\u2019t 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. <\/p>\n<p>With these two design criteria in mind, I came up with the <b>Export-History<\/b> function shown here.<\/p>\n<blockquote>\n<p><strong>Export-History.ps1<\/strong><\/p>\n<p>Function Export-History<\/p>\n<p>{<\/p>\n<p>[CmdletBinding()]<\/p>\n<p>Param(<\/p>\n<p>[Parameter(mandatory=$false,<\/p>\n<p>Position=0,<\/p>\n<p>ValueFromPipeline=$True)]<\/p>\n<p>$CommandInfo,<\/p>\n<p>[Parameter(Mandatory=$true,<\/p>\n<p>Position=1,<\/p>\n<p>HelpMessage=&quot;Type Path for history file&quot;)]<\/p>\n<p>$Path)<\/p>\n<p>if(!$commandInfo)<\/p>\n<p>{<\/p>\n<p>Get-History | Export-Clixml -Path $path<\/p>\n<p>}<\/p>\n<p>Else<\/p>\n<p>{ $input | Export-Clixml -Path $path }<\/p>\n<p>} #end function Export-History<\/p>\n<p># Get-History (6,7) | Export-History -Path c:\\fso\\mycommands.xml<\/p>\n<p># Export-History -Path c:\\fso\\a.xml<\/p>\n<\/blockquote>\n<p>The <b>Get-History<\/b> function begins by creating two parameters. The first parameter is the <b>commandInfo<\/b> parameter. It will accept a value from the pipeline, and is not mandatory. The second parameter is the <b>path<\/b> parameter, and it is mandatory. It tells the function where to save the XML file. This portion of the function is shown here. <\/p>\n<blockquote>\n<p>Function Export-History<\/p>\n<p>{<\/p>\n<p>[CmdletBinding()]<\/p>\n<p>Param(<\/p>\n<p>[Parameter(mandatory=$false,<\/p>\n<p>Position=0,<\/p>\n<p>ValueFromPipeline=$True)]<\/p>\n<p>$CommandInfo,<\/p>\n<p>[Parameter(Mandatory=$true,<\/p>\n<p>Position=1,<\/p>\n<p>HelpMessage=&quot;Type Path for history file&quot;)]<\/p>\n<p>$Path)<\/p>\n<\/blockquote>\n<p>Now I need to add a little bit of logic. If the <b>commandInfo<\/b> 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 <b>Export-History<\/b> 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:<\/p>\n<blockquote>\n<p>if(!$commandInfo)<\/p>\n<p>{<\/p>\n<p>Get-History | Export-Clixml -Path $path<\/p>\n<p>}<\/p>\n<p>Else<\/p>\n<p>{ $input | Export-Clixml -Path $path }<\/p>\n<p>} #end function Export-History<\/p>\n<\/blockquote>\n<p>At the bottom of the Export-History.ps1 file, I have two examples of calling the function. One illustrates calling the function with no <b>commandInfo<\/b>, and the other pipelines the <b>commandInfo<\/b> to the function. These two commands are shown here:<\/p>\n<blockquote>\n<p># Get-History (6,7) | Export-History -Path c:\\fso\\mycommands.xml<\/p>\n<p># Export-History -Path c:\\fso\\a.xml<\/p>\n<\/blockquote>\n<p>So, how do you actually use the <b>Export-History<\/b> 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 <b>path<\/b> parameter as mandatory. <\/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\/4555.wes-1-22-11-1_56ECC4AD.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 dot-sourcing the function\" border=\"0\" alt=\"Image of dot-sourcing the 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\/4555.wes-1-22-11-1_thumb_09702220.jpg\" width=\"604\" height=\"162\" \/><\/a><\/p>\n<p>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. <\/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\/8272.wes-1-22-11-2_0C35D713.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 viewing the XML file in Internet Explorer\" border=\"0\" alt=\"Image of viewing the XML file in Internet Explorer\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/7282.wes-1-22-11-2_thumb_3EB93485.jpg\" width=\"604\" height=\"456\" \/><\/a><\/p>\n<p>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. <\/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\/2806.wes-1-22-11-3_35E92C39.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=\"Imge of the function in the PowerShell ISE\" border=\"0\" alt=\"Image of the function in the PowerShell ISE\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/4555.wes-1-22-11-3_thumb_419E736B.jpg\" width=\"604\" height=\"472\" \/><\/a><\/p>\n<p>You can leave the two commands commented out, and simply run the <b>Export-History<\/b> 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\n 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).<\/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\/3808.wes-1-22-11-4_64130226.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 loading the function onto the function drive\" border=\"0\" alt=\"Image of loading the function onto the function drive\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/1348.wes-1-22-11-4_thumb_5DEBB58B.jpg\" width=\"604\" height=\"472\" \/><\/a><\/p>\n<p>You could also include the <b>Export-History<\/b> function in your Windows PowerShell profile. This is the option I like the best. I have written <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/windows+powershell\/getting+started\/profiles\/\">several Hey, Scripting Guy! Blog posts that talk about using the Windows PowerShell profile<\/a>. Refer to them for ideas about incorporating the <b>Export-History<\/b> function. Join me tomorrow as I develop an <b>Import-History<\/b> function. <\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> or <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>. <\/p>\n<p>&#160;<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#160; 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 [&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":[],"class_list":["post-15831","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting"],"acf":[],"blog_post_summary":"<p>&#160; 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15831","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=15831"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15831\/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=15831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=15831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=15831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}