Skip to content

Instantly share code, notes, and snippets.

@SQLvariant
Last active October 11, 2020 19:06
Show Gist options
  • Save SQLvariant/5d01a508574455412ec8d6c712c06cec to your computer and use it in GitHub Desktop.
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! :-)

@jdhitsolutions
Copy link

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