5

I can export my Powershell history using the scripts outlined in the Technet page on the subject,

Get-History | Export-Clixml ~\PowerShell\history.xml

and I can automatically load up the saved history when I start a Powershell session using this line in my powershell profile,

Import-Clixml ~\PowerShell\history.xml | Add-History

But is there any way that my history can be automatically saved when I exit the session? I'm using ConEmu as my console.

1

Here's an alternative approach that persists your history without using a different PowerShell shortcut.

Taken from https://software.intel.com/en-us/blogs/2014/06/17/giving-powershell-a-persistent-history-of-commands

Open PowerShell in administrator mode and find out which version of PowerShell you have by running for following command:

$PSVersionTable.PSVersion

You'll need a version higher than 3 in order to run this, If you have an older version, update it.

Allow PowerShell to import or use scripts including modules by running the following command:

set-executionpolicy remotesigned

Install PsGet and install the modules you need by executing the following commands:

(new-object Net.WebClient).DownloadString("http://psget.net/GetPsGet.ps1") | iex
import-module PsGet
install-module PsUrl
install-module PSReadline

Create the following script

$HistoryFilePath = Join-Path ([Environment]::GetFolderPath('UserProfile')) .ps_history
Register-EngineEvent PowerShell.Exiting -Action { Get-History | Export-Clixml $HistoryFilePath } | out-null
if (Test-path $HistoryFilePath) { Import-Clixml $HistoryFilePath | Add-History }
# if you don't already have this configured...
Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward

Save the file as C:\Users\<username>\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1, where you should replace with the correct folder name on your PC. Do not forget to change the “save as type” option to all files.

Close PowerShell and open it again so that it starts using the script we saved.

From now on,

get-history

Will retrieve the last few hundred commands that you typed in, and you can scroll between them with the arrow keys.

  • 1
    External links can break or be unavailable, in which case your answer would not be useful. Please include the essential information within your answer and use the link for attribution and further reading. Thanks. – fixer1234 Oct 21 '15 at 16:16
  • I don't have the time but thought it might still be helpful to someone. I've marked it as community wiki so go for it. – Rory Oct 21 '15 at 16:25
  • Then just post the links as comments instead. Don't leave it for others to improve if you don't have the time to give attention to it. – Jamal Oct 21 '15 at 18:42
  • 1
    Paraphrased as far as I can, and updated as a standalone answer. Took me 5 minutes over breakfast. It would be nice if folk both fixed up their own answers, and helped out where possible, I've had some answers that were awesomely improved by others, and that's why we have edit rights, no? – Journeyman Geek Oct 22 '15 at 0:42
  • This actually worked. I'm not sure what the install-module PsUrl step is for, it failed on my machine but the script works anyway. It uses PSReadline, but doesn't seem to use PsUrl – stib Oct 23 '15 at 2:47
5

Another way to do it, just in case it's helpful for anyone.

Powershell now stores command history automatically in the file "%userprofile%\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline\ConsoleHost_history.txt"

To display the history, I've added a function into my profile like so...

function h2 {
    $h = [System.Environment]::ExpandEnvironmentVariables("%userprofile%\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline\ConsoleHost_history.txt")
    cat $h;
}

Now I can just type "h2" to display the full history. Not particularly clever, but effective.

| improve this answer | |
3

Not perfect, but you can use a shortcut with a start command telling PowerShell to save all history on exit.

  1. Right-click your desktop and select New -> Shortcut
  2. Enter the following as the shortcut path:
powershell -NoExit -Command $histlog = Register-EngineEvent -SourceIdentifier ([System.Management.Automation.PsEngineEvent]::Exiting) -Action {Get-History | Export-Clixml ~\PowerShell\history.xml}
  1. Choose a name for your shortcut and click OK.

If you launch from this shortcut, your history will be saved to ~\PowerShell\history.xml whenever PowerShell gets an exit event.

| improve this answer | |
  • Great! thanks. If you don't want to use the default powershell console (say you want to use ConEmu) you can add this to the default tasks that run when you start a new console. – stib Feb 20 '15 at 6:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.