How to View Command History from Previous PowerShell Sessions in Windows
Is there a way to view the command history across all PowerShell sessions? Windows PowerShell allows you to view every command you have executed in the current session using the Get-History command. But sometimes that is not enough. In this tutorial, Greencloud will show you how to view the entire command history from all previous sessions in Windows
1. To use the PowerShell command history viewing functionality, you first need to install the PSReadLine module using the command below
Install-Module PSReadLine
If you are prompted to install NuGet Provider, type Y and press Enter
2. Next, enter the following command to display the path to the file where the PowerShell command history is saved.
(Get-PSReadlineOption).HistorySavePath
3. To view the complete detailed command history on the PowerShell console, run this command
cat (Get-PSReadlineOption).HistorySavePath
4. To clear all history of PowerShell commands that you have entered, type the following command
Remove-Item (Get-PSReadlineOption).HistorySavePath
5. If you need to prevent PowerShell from saving command history, execute this command
Set-PSReadlineOption -HistorySaveStyle SaveNothing
6. Whenever you want to configure PowerShell to trace back all executed commands, run the following command
Set-PSReadlineOption -HistorySaveStyle SaveIncrementally
Conclusion
Above are the commands you can use to control the history of commands used in Powershell.
Good luck!