Whoa! I just spent an enjoyable half-hour learning about the various PowerShell command line history viewing and editing tools. This comes courtesy of OhMyPosh creator Jan De Dobbeleer (@jandedobbeleer) on Twitter. Deciphering PowerShell history commands, in my case, involved a fair amount of interesting play and learning in a Terminal session. As you can see from the lead-in graphic, I had fun manipulating my command history (and then, updated OhMyPosh to catch up my test system).
When Deciphering PowerShell History Commands, Do This…
The operative way to understand PS history management is as a series of prefixes to “-history” at the command line — namely:
- get: shows current PS command line history as stored for display
- clear: clears current PS command line history
- add: allows you to import a predefined command history from a file
There’s a lot more to managing history than you might think, as described in this MS Learn reference on the Clear-History command. Indeed you can tailor the history based on commands by number (from top or bottom of the history list, using -Count and other options) or by content (using the -CommandLine option and string-matching facilities).
Wait! There’s an Add-History, Too
You can save a representative command history by piping get-history into a CSV file. Later on, Add-History lets you import that file’s contents to imbue the current command history into your current PowerShell context. See this reference for more info.
Working with PowerShell history commands is great fun, actually. I’d suggest visiting the afore-linked references to take things for a spin. I find it useful to clear the history after such learning adventures (or after making mistakes at the command line that I’d just as soon forget…).