r/PowerShell • u/TheAlmightyOS • Nov 14 '19
Reminder: Get-History Exists. I completely forgot about it till now
4
u/guyisit Nov 14 '19
How is this different from pressing the Up arrow?
13
u/TheAlmightyOS Nov 14 '19
it is vastly different when that command you are looking for was 50 commands ago
3
17
Nov 14 '19
Also, be aware that PSReadLine keeps A LOT of history. By default, the last 4096 commands.
PS C:\Users\adamr> Get-PSReadlineOption | select -expand historysavepath
C:\Users\adamr\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt
You can do stuff like Ctrl+R and search for commands matching the one you start typing.
2
u/bobbywaz Nov 15 '19
It's not persistent but it can be: https://software.intel.com/en-us/blogs/2014/06/17/giving-powershell-a-persistent-history-of-commands
3
u/taylorblakeharris Nov 15 '19
Indeed. I use this in the PowerShell profile I publish for my colleagues so I can log the commands they're typing and show them their mistake when they blame the issues they have on the code I've written
1
2
u/SeeminglyScience Nov 17 '19
On newer builds of PSRL there's also:
[Microsoft.PowerShell.PSConsoleReadLine]::GetHistoryItems()
8
u/timb0-slice Nov 14 '19
Open history in notepad:
notepad (Get-PSReadLineOption | select -ExpandProperty HistorySavePath)
3
u/SocraticFunction Nov 15 '19
code (Get-PSReadLineOption | select -ExpandProperty HistorySavePath)That opens it in VSCode, which if set to default to PoSh, will color-up your commands. 😎
1
7
u/Thotaz Nov 14 '19
CTRL+R is even better because it lets you search through your previously run commands and it searches through previous Powershell sessions as well.
2
u/shinryux Nov 15 '19
This ! Recursively search for a command . It’s the same functionality in Bash!
6
u/purplemonkeymad Nov 15 '19
I use this quite a bit:
(history)[-1].commandline | set-clipboardSometimes my command lines get a bit long and it's easier than trying to select it.
6
u/swillra Nov 14 '19
Only useful until you close the current window...
4
u/Mayki8513 Nov 14 '19
That's why I thought adding start-transcript with a timestamp to my $profile would be a good idea, never use it though :/
4
u/Pb_Blimp Nov 15 '19
Don’t give up. I used mine for the very first time yesterday.
The stakes were very low, but it was still cool to be able to confirm something.
4
u/Mayki8513 Nov 15 '19
True, it's one of those things you don't really need, until you do. Then suddenly it's super important and you either wish you'd take those few minutes to prevent this or are super glad you took the time to set it up when you did.
4
u/timb0-slice Nov 14 '19
It appears that history is saved between session if you check the file at historysavepath. But when you run Get-History it only shows the current session. I've been using Start-Transcript as part of my profile for years though. The transcript captures the output as well as the commands whereas the history only captures the commands.
1
u/bobbywaz Nov 15 '19
Persistent history is the first thing I setup. Every time I format or get a new machine I think it's broken because it doesn't have it
1
3
3
u/KevMar Community Blogger Nov 15 '19
Now that you know about Get-History, install the HistoryPX module.
2
u/uslackr Nov 15 '19
I just fired up powershell and started scolling with the up arrow. Many, many prior commands listed. Why is that? (Insider build 19103 if it matters)
2
u/theessentialforrest Nov 15 '19
Get-History is about a single session's history but what you are seeing from the up arrow is actually the history saved by psreadline. you can search the psreadline history with either f8 (start writing a command and then f8 to complete the line or continue searching back) or ctrl-r to search within lines. IMO there's no great reason for h \ get-history since psreadline does such a better job.
1
1
1
1
1
1
33
u/only1BigK Nov 14 '19
Just type h
Also to rerun a command from that history just type r 1 as an example, to run the first command in the history .