One of the main drawbacks of Powershell command prompt compared to bash was the inability to keep a history of executed commands. The command history could be viewed only in the current Powershell session, and when Powershell console has been closed or the computer has been restarted, the command history is not saved. This drawback has been fixed in PowerShell v5, which is integrated into Windows 10.
Now when you press the up arrow key you can go to the last command you entered or to the previous ones (down arrow). By default, PowerShell remembers 4096 commands stored in a plain text file in the profile of each user %userprofile%\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline\ConsoleHost_history.txt. The history is stored separately for PowerShell and ISE.
You can search the command history using CTRL+R (reverse search) or CTRL+S (forward search). The text you entered will be found in the command history in any position (unlike search in PowerShell using F8 or Shift+F8, which allow to look for the matches from the beginning of the line only) and a suitable variant will be highlighted.
The command history can be configured using Get-PSReadlineOption and Set-PSReadlineOption cmdlets. Current settings can be viewed using this command:
Get-PSReadlineOption | select HistoryNoDuplicates, MaximumHistoryCount, HistorySearchCursorMovesToEnd, HistorySearchCaseSensitive, HistorySavePath, HistorySaveStyle
The settings of the following parameters can be important for us:
HistoryNoDuplicates – determines whether the same commands have to be saved
MaximumHistoryCount –the maximum number of the stored commands
HistorySearchCursorMovesToEnd — determines whether you have to go to the end of the command when searching
HistorySearchCaseSensitive – determines whether search is case sensitive
HistorySavePath – shows the file the command history is stored
HistorySaveStyle – determines the peculiarities of saving commands:
- SaveIncrementally — the commands are saved after they are run (by default)
- SaveAtExit — the history is saved when the console is closed
- SaveNothing — disables saving command history
You can change the settings of PSReadLine module using Set-PSReadlineOption, for instance:
Set-PSReadlineOption -HistorySaveStyle SaveAtExit
So, the ability to save the history of PowerShell commands is one of the arguments to prefer PoSh 5.0 to cmd.