3

I use Microsoft's PowerShell for remoting into machines (usually with OpenSSH for Windows), managing Git repositories, and running commands in VS Code (and other things).

I usually use command history to go back and select SSH commands for remoting, but after long sessions with Git or VS Code the SSH history ends up being pretty far back. Is there a way to launch PowerShell with a different history file so that the command history is different for each task?

There is a history file stored in %AppData%\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt, so maybe there's an environment variable or command line switch that can set it to a different location for the session (by launching PowerShell through a batch file for example).

3

You can set the history file location with Set-PSReadlineOption. If you want to switch you could make a couple of .ps1 files, for example:

History1.ps1 containing

Set-PSReadlineOption -HistorySavePath 'C:\Users\Hali\Desktop\History1.txt'

DefaultHistory.ps1 containing

Set-PSReadlineOption -HistorySavePath 'C:\Users\Hali\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt'

and create aliases to them. For example:

Set-Alias -Name History1 -Value 'C:\<path to>\History1.ps1'
Set-Alias -Name DefaultHistory -Value 'C:\<path to>\DefaultHistory.ps1'
1
  • 1
    Works perfectly. I stuck start powershell -noexit Set-PSReadlineOption -HistorySavePath 'History SSH.txt' in a batch file and now I have an easy way to open shells with different histories. – user Dec 31 '19 at 18:52

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.