Windows PowerShell is incredibly easy to administrate once you learn the basics of the scripting language. Despite that, users might encounter a few hiccups here and there. Many of them have reported that PowerShell is not accepting the Ctrl+R shortcut to show the command history search.
Since this essentially means that users can no longer enjoy the history function, it’s definitely unpleasant. PowerShell has a pretty great built-in command history to help you out with those complex lists that you have entered over time, but if you can’t use it, that’s a big problem.
If you are troubled by this issue too, to get around it we recommend the following.
What can I do when PowerShell history won’t work?
1. Check the PSReadLine assembly
Many others have experienced the same problem after upgrading to Windows 10. You might not know that you’ve installed PSReadLine, but it comes bundled with Windows 10, so accept that it’s there.
Like it or not, it pretty much replaces the old-style command history features with more sophisticated functionality. The command history function in PowerShell is not built into the Windows Management Framework itself, but it’s based precisely on the third-party PSReadLine module.
To confirm it has any involvement this time, try to run Get-PSReadLineOption and note if you get this error regarding the PSReadLine assembly:
Get-PSReadLineOption : The type initializer for ‘Microsoft.PowerShell.PSConsoleReadLine’ threw an exception.
At line:1 char:1
+ CategoryInfo : NotSpecified: (:) [Get-PSReadLineOption], TypeInitializationException
+ FullyQualifiedErrorId : System.TypeInitializationException,Microsoft.PowerShell.GetPSReadLineOption
If you see the above lines too, close PowerShell, then change keyboard layout to en-US. Open it back and check if PowerShell history works again.
2. Downgrade PSReadLine to 1.2
Upgrading PSReadLine to a new version could also cause this error you’re getting. This mostly happens if you are running PowerShell with a non en-US keyboard layout.
However, if the above steps made no difference, you could also try to downgrade PSReadLine to 1.2.
3. Try out alternative cmdlets to view the history
Suppose you typed and executed some complicated PowerShell commands and you want to take a peek now. You should be able to browse the entire history of PowerShell commands using the up / down arrows.
As always, you can also use several included cmdlets to work with your history. To view the history of commands you’ve typed, run the following cmdlet:
You may further search your history by piping the resulting output to the Select-String cmdlet. Don’t forget to replace “Search” in the cmdlet below with the actual text you want to search for:
Get-History | Select-String -Pattern “Search”
In order to view more detailed information about previously executed commands, including the command status or start/end time, run this command instead:
Get-History | Format-List -Property *
No less important, if things do work and you want to export commands from the current session to a file, use the Export-Clixml cmdlet:
Get-History | Export-Clixml -Path c:pscommands_hist.xml
4. Increase number of remembered commands
Remember that the command line buffer keeps only the last 50 commands you typed. If wanting more is the actual problem you’re facing, just increase this number.
To make it happen, right-click the title bar of the PowerShell prompt window, select Properties, then simply change the value of Buffer Size under Command History.
A potential cause of this error is that Powershell‘s history is turned off. Here’s where you might have entered before:
You could have turned it off with:
Set-PSReadlineOption –HistorySaveStyle SaveNothing
Does it ring any bells? The same goes if you’re looking for something after you fully clear the memory in the hope of a fresh start.
Just be careful: the Clear-History cmdlet can’t really be used to clear the history of commands. It simply clears the list of commands that the Get-History cmdlet displays.
Following the procedures mentioned in this article should help you to resolve the issue you were experiencing. If you’re aware of any other way to make PowerShell history work again, do tell us about it in the comments section below.