Skip to content

Instantly share code, notes, and snippets.

@SQLvariant
Last active October 11, 2020 19:06
Show Gist options
  • Select an option

  • Save SQLvariant/5d01a508574455412ec8d6c712c06cec to your computer and use it in GitHub Desktop.

Select an option

Save SQLvariant/5d01a508574455412ec8d6c712c06cec to your computer and use it in GitHub Desktop.
I wish there was some kind of -GridThru functionality (`Out-GridView` with `-PassThru`) built into the Get-History command

Please 'Code Golf' this snippet

I like to get my code 'working' and then see if I can shorten the code, make it run faster, or maybe just reduce the number of variables I'm using.

However, sometimes I end up breaking things. At which point, I want to jump back to a version of the code that executed successfully. To do that, I sometimes use the code block below:

Get-History |
SELECT * |
Out-GridView -PassThru |
foreach { $_.CommandLine } | clip

I wish there was some kind of -GridThru functionality (Out-GridView with -PassThru) built into the Get-History command, but there isn't. I also realize I can put this into a function to shorten things up, but then I have to remember to run that function where I'm going to use it.

So with that said, I'm curious how much shorter I coule get this command (like swapping out Out-GridView with ogv)?

Can you please suggest the shortest thing you can come up with?

Functionality goals

  • Allow the user to see all of their command execution history
  • Allow the user to select the full command syntax of one (or maybe more) or their previously executed commands
  • Send the resulting selection to the clipboard

Note: When the code gets to the clipboard, I want to be able to paste it right back into my PowerShell editor and run it, without any extra steps.

Additional Note: To help me find the correct piece of code to grab, I need to tell that it had completed successfully (ExecutionStatus -eq 'Completed'), or just have that pre-filtered for me.

I can't wait to see what y'all come up with. I will update this gist with the 'leading' (read: shortest working) version of the code, and let everyone know who came up with it. Thanks in advance to everyone who tries! :-)

@romero126

romero126 commented Jul 9, 2020

Copy link
Copy Markdown

Or you can just type
#<ctrl+Spacebar>
and stop wasting your time with silly codegolf shenanigan's when we have PSReadline and its tab completions to solve our problem for us.

@Jaykul

Jaykul commented Jul 9, 2020

Copy link
Copy Markdown

Yeah, it's probably worth pointing out that what I normally do for this sort of thing is run h and scan the output, and then just enter the number from the history id and press tab, like #86{Tab}

However, the OGV method is a lot cleaner in the scrollback ;-)

@SQLvariant

Copy link
Copy Markdown
Author

Yeah, it's probably worth pointing out that what I normally do for this sort of thing is run h and scan the output, and then just enter the number from the history id and press tab, like #86{Tab}

Oh wow @Jaykul, thanks for sharing that. I learned something new today!
Your h|select *|ogv -p|% C*|scb submission seems to be the shortest that meets all the requirements, so far.

Using the #86{Tab} you mentioned, or the #ctrl+Spacebar @romero126 mentioned are excellent to know about. Two of the other reasons I'm looking for the shortest code possible, so I can memorize it, are:

  1. I help a lot of people when they get stuck coding, so I expect to need to tell someone else this sequence, to use on their own machine.
  2. I will be using this in Jupyter Notebooks where the # bit doesn't work. (or at least, it doesn't work as of today.)

@Jaykul

Jaykul commented Jul 9, 2020

Copy link
Copy Markdown

Yeah, in Jupyter you don't get PSReadLine or any of it's features

@romero126

Copy link
Copy Markdown

You should be able to do R 13 <the line #> as well

@eliassal

Copy link
Copy Markdown

HI, I am not so expert like you but I have a question, is there any possible way to get the history after closing and opening a PS session like linux bash?

@jdhitsolutions

Copy link
Copy Markdown

If you are using the PSReadline module and have it configured to maintain history, which I think is the default, then yes, you can access history between sessions. Use Ctrl+R to search, just as you do in Linux.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment