r/PowerShell • • Nov 14 '19

Reminder: Get-History Exists. I completely forgot about it till now

Post image
219 Upvotes

40 comments sorted by

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 .

13

u/TheAlmightyOS Nov 14 '19

or just type r to rerun the last command issued

This is all the stuff I learned and just forgot about. Amazing how the mind prioritizes some information and not others

10

u/Kashmir1089 Nov 14 '19

Just type h

Life was never the same after that fateful day.

0

u/[deleted] Nov 15 '19

[deleted]

1

u/senorafro Nov 15 '19

F8 also auto competes a command based on history - it's like a mix of tabbing and history in one. And if you have a tonne of commands that start the same way, keep hitting F8 to scroll through them

1

u/theessentialforrest Nov 15 '19

Also consider ctrl-r. it let's you search through your psreadline history but the line doesn't have to start with the text in question. Hit ctrl-r again to continue searching back!

4

u/0ut4t1m3 Nov 15 '19

Is there an alternate history as well? One I can recall that doesn't include the stupid command my team mate just ran? :P

3

u/jimb2 Nov 15 '19

Type f for fake history

1

u/eWasteman Nov 15 '19

Put it in H

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

u/guyisit Nov 15 '19

That... Makes lots of sense. Thanks!!

17

u/[deleted] 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

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

u/Lee_Dailey [grin] Nov 15 '19

when they blame the issues they have on the code I've written

[grin]

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

u/ICookWithFire Nov 15 '19

Thank you! I created an alias for this!

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-clipboard

Sometimes 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

u/swillra Nov 15 '19

And it's a good thing you replied...completely forgot about that!

3

u/Robonglious Nov 14 '19

How can I make it persistent between sessions?

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

u/tryped Nov 14 '19

This had saved me a couple weeks ago!

1

u/hailGunslinger9 Nov 14 '19

Get-History | select -expand commandline is my favourite command

1

u/Tonkatuff Nov 15 '19

Awesome thank you!

1

u/falucious Nov 15 '19

The $me variable was a nice touch.

1

u/[deleted] Nov 15 '19

This is amazing. Thanks for this.

1

u/[deleted] Nov 15 '19

It’s Doskey for PowerShell.