Powershell stopped using its persistent history (Windows 11). When I start a new Powershell instance commands from previous sessions don't show up. Once I use commands in the new session for the first time they can be recalled via F8/arrows etc.
What I checked/found out so far:
Checking the history file
C:\Users\[username]\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txtI noticed commands do not get saved there (anymore).Once I enter the command
(Get-PSReadLineOption).HistorySavePath(which returns the path I mentioned in my previous bullet point) everything works fine again. Commands already saved there are accessible again via F8 etc. and new commands get saved into the history file again as well.Using
powershell -noexit Set-PSReadlineOption -HistorySavePath "C:\Users\admin\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt"to start powershell via a script works as well.
I haven't found out exactly why Powershell isn't using the history file by default anymore. As far as I remember it used to work earlier and I'm not sure why it stopped working. But it was already some time ago judging by the content of the history file.
Any idea how I can get it working again without a workaround that has to be applied on every startup (like typing in the "history save command" manually or using a "parameter to start it")?
Addition 1:
These commands give the following output (note: after using any of them the history works fine again, commands get saved to history and the full can be recalled except for the first command that has been used and "reactivated" the history)
PS C:\> (Get-PSReadlineOption).HistorySaveStyle
SaveIncrementally
PS C:\> (Get-PSReadlineOption).HistoryNoDuplicates
True
PS C:\> (Get-PSReadlineOption).MaximumHistoryCount
4096Addition 2:
$PROFILE gives me the following output (the command does not affect the history's behavior). Note: the files listed do not exist within the indicated directories:
PS C:\> $PROFILE | Get-Member -Type NoteProperty TypeName: System.String
Name MemberType Definition
---- ---------- ----------
AllUsersAllHosts NoteProperty string AllUsersAllHosts=C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
AllUsersCurrentHost NoteProperty string AllUsersCurrentHost=C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts NoteProperty string CurrentUserAllHosts=C:\Users\username\Documents\WindowsPowerShell\profile.ps1
CurrentUserCurrentHost NoteProperty string CurrentUserCurrentHost=C:\Users\username\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1Addition 3 - including a workaround:
Seems the PSReadLine module isn't loading. Using Import-Module PSReadLine makes the history work properly. As a workaround, I put the command into the PS profile file, so it gets executed automatically which fixes the problem but not the cause. This is probably also the reason why the history starts working once a command relating to the modulus is used, I assume PowerShell loads the module by itself.
Addition 4:
The problem is specific to the user profile. In a new user profile, the history works fine. Standard checks using sfc, dism and chkdsk didn't yield any results.
1 Answer
As noted in my question, as a workaround I currently use Import-Module PSReadLine in my profile file, which I access via notepad $PROFILE. Since the problem only exists for my profile, this does the trick for my user without affecting any other user.
Although this does not resolve the problem - for now it fixes all my current issues.