{"id":10081,"date":"2006-07-01T01:37:00","date_gmt":"2006-07-01T01:37:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2006\/07\/01\/perserving-command-history-across-sessions\/"},"modified":"2019-02-18T13:21:35","modified_gmt":"2019-02-18T20:21:35","slug":"perserving-command-history-across-sessions","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/perserving-command-history-across-sessions\/","title":{"rendered":"Perserving Command History Across Sessions"},"content":{"rendered":"<p>&lt;Edited 7\/2\/2006 to add tags and Categories&gt;<\/p>\n<p>Ben Winzenz didn&#8217;t like the fact that Windows PowerShell did not maintain history lists between sessions (<a href=\"http:\/\/winzenz.blogspot.com\/2006\/06\/cool-mshpowershell-tidbit.html\">http:\/\/winzenz.blogspot.com\/2006\/06\/cool-mshpowershell-tidbit.html<\/a>)&nbsp;.&nbsp;&nbsp; We hear you Ben.&nbsp; Back to my least favorite phrase, &#8220;to ship is to choose&#8221;.&nbsp; That said, we try to give you in the community the power to do that which we cannot.&nbsp; Here is some code that you can put in your profile file that defines a function BYE which saves off your sessions history into a History.CSV file in your home directory and then adds that to your history when you startup the next session.&nbsp; You get to control how much of the history you want (up to 32KB-1) but I just do 1KBs worth.<\/p>\n<p><font face=\"Courier New\" size=\"1\">$MaximumHistoryCount = 1KB<\/font><\/p>\n<p><font face=\"Courier New\" size=\"1\">if (!(Test-Path ~\\PowerShell -PathType Container))<br \/>{&nbsp;&nbsp; New-Item ~\\PowerShell -ItemType Directory<br \/>}<\/font><\/p>\n<p><font face=\"Courier New\" size=\"1\">function bye <br \/>{&nbsp;&nbsp; Get-History -Count 1KB |Export-CSV ~\\PowerShell\\history.csv<br \/>&nbsp;&nbsp;&nbsp; exit<br \/>}<\/font><\/p>\n<p><font face=\"Courier New\" size=\"1\">if (Test-path ~\\PowerShell\\History.csv)<br \/>{&nbsp;&nbsp;&nbsp;Import-CSV ~\\PowerShell\\History.csv |Add-History<br \/>}<br \/><\/font><\/p>\n<p>I was going to put comments in the code but thought I would test out my assertion that VERBOSITY produces self-documenting scripts.&nbsp; I think it does but you can let me know one way or the other.<\/p>\n<p>So add this to your startup and then do a session, type BYE instead of EXIT and then start a new session and do a Get-History.&nbsp;&nbsp; You&#8217;ll see a bunch of commands already in your list.&nbsp; <\/p>\n<p>Now lets have a little fun with History, do what I instructed above and get into your new session.&nbsp; Now do a Get-History and pipe it to Get-Member:<\/p>\n<p><font face=\"Courier New\" size=\"1\">PS&gt; Get-History |Get-Member -MemberType Property<br \/>&nbsp;&nbsp; TypeName: Microsoft.PowerShell.Commands.HistoryInfo<\/font><\/p>\n<p><font face=\"Courier New\" size=\"1\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MemberType Definition<br \/>&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-<br \/>CommandLine&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; System.String CommandLine {get;}<br \/><strong><font color=\"#ff0000\">EndExecutionTime<\/font><\/strong>&nbsp;&nbsp; Property&nbsp;&nbsp; System.DateTime EndExecutionTime {get;}<br \/>ExecutionStatus&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; System.Management.Automation.Runspaces.Pip&#8230;<br \/>Id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; System.Int64 Id {get;}<br \/><font color=\"#ff0000\"><strong>StartExecutionTime<\/strong><\/font> Property&nbsp;&nbsp; System.DateTime StartExecutionTime {get;}<\/font><\/p>\n<p>This means that you can find out when something was executed (e.g. which session it happened in) using the following command:<\/p>\n<p><font face=\"Courier New\" size=\"1\">PS&gt; ghy |ft id,endexecutiontime,commandline -auto<\/font><\/p>\n<p><font face=\"Courier New\" size=\"1\">&nbsp;Id EndExecutionTime&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CommandLine<br \/>&nbsp;&#8212; &#8212;&#8212;&#8212;&#8212;&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8211;<br \/>612 6\/29\/2006 5:39:34 AM&nbsp; gcm export-csv |fl *<br \/>613 6\/30\/2006 6:51:16 PM&nbsp; ipconfig<br \/>614 6\/30\/2006 8:51:38 PM&nbsp; cd C:\\kits<br \/>&#8230;<\/font><\/p>\n<p>Enjoy!<br \/>Jeffrey P. Snover<br \/>Windows PowerShell Architect<\/p>\n<p>PSMDTAG:FAQ: How do I perserve command history across sessions?<\/p>\n<p>PSMDTAG:FAQ:&nbsp;&nbsp;Can I run an exit script?<\/p>\n<p>PSMDTAG:ENVIRONMENT:&nbsp;History<\/p>\n<p>PSMDTAG:SHELL: shutdown, startup, exitScript<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&lt;Edited 7\/2\/2006 to add tags and Categories&gt; Ben Winzenz didn&#8217;t like the fact that Windows PowerShell did not maintain history lists between sessions (http:\/\/winzenz.blogspot.com\/2006\/06\/cool-mshpowershell-tidbit.html)&nbsp;.&nbsp;&nbsp; We hear you Ben.&nbsp; Back to my least favorite phrase, &#8220;to ship is to choose&#8221;.&nbsp; That said, we try to give you in the community the power to do that which [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[29,37,10,38,39,40,21],"class_list":["post-10081","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-environment","tag-export-clixml","tag-faq","tag-get-history","tag-import-clixml","tag-sessions","tag-shell"],"acf":[],"blog_post_summary":"<p>&lt;Edited 7\/2\/2006 to add tags and Categories&gt; Ben Winzenz didn&#8217;t like the fact that Windows PowerShell did not maintain history lists between sessions (http:\/\/winzenz.blogspot.com\/2006\/06\/cool-mshpowershell-tidbit.html)&nbsp;.&nbsp;&nbsp; We hear you Ben.&nbsp; Back to my least favorite phrase, &#8220;to ship is to choose&#8221;.&nbsp; That said, we try to give you in the community the power to do that which [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10081","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=10081"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10081\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=10081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=10081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=10081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}