How To Get The PowerShell Command History On Windows 10
PowerShell on Windows 10 can give you a history of every command you’ve executed in the current session however, for many users this isn’t enough. They need a history of commands executed across sessions. If you need to get the PowerShell command history, and history for the current session alone doesn’t do the trick, there’s a script and a log file that can help you.
Command History Current Session
If you’re running PowerShell 5, you can get the command history for the current session by running the following command;
Get-History
By default, PowerShell can save up to 50 commands but you can change it to save more. Open PowerShell and right-click the title bar. From the context menu, go to Properties. On the Properties window, go to the Options tab. You will see a Command History section where the default is set to 50. Change it to a higher value.
PowerShell History Log
In order to view the history log, you need to first install the PSReadLine module with the following command;
Install-Module PSReadLine
Next, run the following command and it will return a file location. The file at this location will give you a complete history of every command you’ve run in PowerShell.
(Get-PSReadlineOption).HistorySavePath
Cross-Session PowerShell Command History
This is a bit of a long process but it’s worth it. This solution comes from Intel. First open the following location and check if there is a file named Microsoft.PowerShell_profile.ps1 at this location.
C:\Users\<username>\Documents\WindowsPowerShell
If there’s no file there, open PowerShell and run the following command. It will open your Profile file in Notepad but that file doesn’t exist and PowerShell will tell you as much, and ask if you want to create it. Allow it to create the file.
notepad $Profile
Close the notepad file that opens. In PowerShell, run this command to make sure you’re running PowerShell 5.
$PSVersionTable.PSVersion
Next, set your execution policy to RemoteSigned with the following command.
set-executionpolicy remotesigned
Next, you need to install the PsUrl and PSReadline modules. You can install them with the following command though if you get an error, just install them manually by the process outlined on the linked pages, or try adding the repository as a trusted repository and then running the command again.
install-module PsUrl
install-module PSReadline
Now that those modules have been installed, open this file;
C:\Users\<username>\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
Paste the following inside this file, and save it.
$HistoryFilePath = Join-Path ([Environment]::GetFolderPath('UserProfile')) .ps_history Register-EngineEvent PowerShell.Exiting -Action { Get-History | Export-Clixml $HistoryFilePath } | out-null if (Test-path $HistoryFilePath) { Import-Clixml $HistoryFilePath | Add-History } # if you don't already have this configured... Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward
In order to view command history in PowerShell, you need to run the Get-History command however there is one very crucial step to making sure your history is saved. You CANNOT close PowerShell by clicking the close button. Instead you must always close it with the exit command.
You can use the Up and Down arrow keys to cycle through the previous commands.