Powershell 历史记录不再持久

Alb*_*bin 5 powershell command-history windows-11

Powershell 停止使用其持久历史记录 (Windows 11)。当我启动新的 Powershell 实例时,以前会话中的命令不会显示。一旦我第一次在新会话中使用命令,就可以通过 F8/箭头等来调用它们。

到目前为止我检查/发现的内容:

  • 检查历史文件,C:\Users\[username]\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt我注意到命令不再保存在那里。

  • 一旦我输入命令(Get-PSReadLineOption).HistorySavePath(它返回我在上一个要点中提到的路径),一切都会再次正常工作。已经保存在那里的命令可以通过 F8 等再次访问,并且新命令也会再次保存到历史文件中。

  • powershell -noexit Set-PSReadlineOption -HistorySavePath "C:\Users\admin\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt"通过脚本启动 powershell 也可以。

我还没有确切地知道为什么 Powershell 默认情况下不再使用历史文件。据我记得它以前可以工作,但我不确定为什么它停止工作。但从历史文件的内容来看,已经是很久之前的事了。

知道如何让它再次工作,而不需要在每次启动时应用解决方法(例如手动输入“历史记录保存命令”或使用“参数来启动它”)吗?

补充1:

这些命令给出以下输出(注意:使用其中任何一个后,历史记录再次正常工作,命令被保存到历史记录中,并且除了已使用的第一个命令并“重新激活”历史记录之外,可以调用完整内容)

PS C:\> (Get-PSReadlineOption).HistorySaveStyle
SaveIncrementally
PS C:\> (Get-PSReadlineOption).HistoryNoDuplicates
True
PS C:\> (Get-PSReadlineOption).MaximumHistoryCount
4096
Run Code Online (Sandbox Code Playgroud)

补充2:

$PROFILE给我以下输出(该命令不会影响历史记录的行为)。注意:列出的文件不存在于指定的目录中:

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.ps1
Run Code Online (Sandbox Code Playgroud)

添加 3 - 包括解决方法:

似乎 PSReadLine 模块未加载。使用Import-Module PSReadLine使历史记录正常工作。作为解决方法,我将命令放入 PS 配置文件中,以便它自动执行,从而解决问题,但不能解决原因。这可能也是为什么一旦使用与模数相关的命令历史记录就开始工作的原因,我假设 PowerShell 会自行加载模块。

补充4:

该问题特定于用户配置文件。在新的用户配置文件中,历史记录运行良好。使用 , 进行标准检查sfcdismchkdsk没有产生任何结果。

Alb*_*bin 0

正如我的问题中所指出的,作为我当前在我的配置文件中使用的解决方法Import-Module PSReadLine,我通过notepad $PROFILE. 由于问题仅存在于我的个人资料中,因此这对我的用户有效,而不会影响任何其他用户。

尽管这并不能解决问题 - 目前它解决了我当前的所有问题。