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! :-)
Is
Select *
really needed? Also, useset-clipboard
instead ofclip.exe
to make it cross platform: