Your Command History
PowerShell remembers the commands you type, so the up arrow, Ctrl+R, Get-History and the prediction list bring back a long command instead of making you type it again.
Apprentice · PowerShell · Operating System · Updated
After this lesson you can
- Bring back an earlier command with the up arrow, Ctrl+R, or Get-History and Invoke-History
- Reuse a command from the predictions PSReadLine shows as you type
Key takeaways
- The up arrow brings back your last command, ready to change or run again. Press it again for the one before.
- Ctrl+R searches your history as you type a few letters of a command you ran before.
- Get-History lists this window's commands with numbers, and Invoke-History runs one again by its number.
- PSReadLine saves your history in a file, so the up arrow, Ctrl+R and its gray predictions remember earlier windows too.
Ten minutes ago you typed a long command that went three folders deep and read a file, and now you need it again. Typing it a second time is slow, and every letter is another chance for a typo. You never have to: PowerShell remembers what you type. Experienced people seem fast at the terminal mostly because they almost never type the same command twice.
This lesson uses the trip folder from Where Am I? Get-Location and Paths.
The up arrow
Press the up arrow and your last command appears at the prompt, as if you'd typed it a moment ago. Press it again for the one before, and the down arrow to come back. The command isn't run until you press Enter, so you can change it first: bring back Get-Content itinerary.txt, change itinerary to packing-list, and press Enter. Esc clears the line if you change your mind.
When the screen fills up, Clear-Host (or Ctrl+L) clears it. Your history stays.
Searching your history: Ctrl+R
Twenty presses of the up arrow is no fun. Press Ctrl+R instead, and type a few letters from anywhere in the command, such as trail. PowerShell shows the newest command you ran with those letters in it:
bck-i-search: trail
That's a backward incremental search: backward because it starts from your newest command, incremental because it narrows with every letter you type. Press Ctrl+R again for an older match, and Enter to run the one shown.
The numbered list: Get-History
Get-History lists the commands you've typed in this window, each with a number, its Id:
Set-Location ~\terminal-practice\trip
Get-ChildItem -Name
Get-Content itinerary.txt
Get-History
Id Duration CommandLine
-- -------- -----------
1 0.025 Set-Location ~\terminal-practice\trip
2 0.008 Get-ChildItem -Name
3 0.005 Get-Content itinerary.txt
Duration is how many seconds each command took, so your numbers will differ. Invoke-History runs one again by its Id. It prints the command first, then runs it:
Invoke-History 3
Get-Content itinerary.txt
Day 1: the beach. Swim, build a sandcastle, watch the sunset from the pier.
Day 2: the old town. The market in the morning, the clock tower at noon.
Day 3: the mountains. Hike to the summit, stop at the waterfall on the way.
The history now ends with Get-Content itinerary.txt again, not with Invoke-History 3: it remembers the command that really ran. The short names are h for Get-History and r for Invoke-History.
Predictions: history that types for you
The up arrow and Ctrl+R come from PSReadLine, the part of PowerShell 7 that reads what you type at the prompt. It does one more thing: as you type, it suggests a whole command from your history in gray after the cursor. Type Get-Con, and the rest of Get-Content itinerary.txt appears in gray. Press the right arrow to accept it, or keep typing to ignore it.
Press F2 to switch to a list of several predictions under the prompt, then pick one with the up and down arrows. Press F2 again to go back to the gray line.
Get-History knows only this window, but PSReadLine saves every command to a file, so the up arrow, Ctrl+R and predictions remember yesterday's windows too. This shows where that file is:
(Get-PSReadLineOption).HistorySavePath
C:\Users\Sam\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt
Because it's a file of everything you type, never type a password at the prompt as part of a command.
On a Mac or Linux it all works the same, and the file is in your home folder's .local/share/powershell/PSReadLine folder. Windows PowerShell 5.1 has the up arrow, Ctrl+R and Get-History (without Duration), but no predictions.
Try it in the terminal
Terminal Quest: The Lighthouse, goals 10 to 11: played right on the lesson page.
You're at the top of the tower. Look at your own logbook of commands, then light the lamp before the ships arrive.
Check yourself
2 questions to try on the lesson page.
So what
Your history is a record of everything that worked. Reusing it, with the up arrow, a search or a prediction, is faster than typing and makes far fewer typos.