Recall PowerShell command for edit

2

I know about the Invoke-History command (alias r) to recall and execute a previous command, e.g. r 29 but is there a way to recall command 29 but not execute it, i.e. put it into the current line you can edit before running? A bit like pressing up-arrow multiple times? With a long history, it's a right pain to get back to edit & run and earlier command.

I know I can do something like this to recall the text of a command:

h 56 | fl

And then copy & paste but with long commands wrapping over a line, you have to copy & paste into an editor, join up the lines and then copy & paste back. Right faff!

munrobasher

Posted 2014-03-07T11:38:46.853

Reputation: 640

This doesnt match the criteria for your answer, but DOSKEY.exe works in PowerShell. Maybe it will help a little. http://www.computerhope.com/doskeyhl.htm

– Keltari – 2014-03-07T14:11:56.670

Answers

2

Can not find a way of executing powershell within powershell but you can execute the powershell executable and pass the powershell command and it works. Might cause problems with scope depending what you are doing.

& powershell $(h 56).CommandLine

rob

Posted 2014-03-07T11:38:46.853

Reputation: 575

2

Not a pure Powershell solution, but you could pipe it into clip.exe and then paste it to the console. Still two steps but your line should wrap properly. If you are using XP you might need to copy clip.exe from a Server 2003 system.

(h 56).CommandLine | clip.exe

Spamwich

Posted 2014-03-07T11:38:46.853

Reputation: 301

Shouldn't that be $(h 56). Also, could use Set-Clipboard instead of clip.exe. Good idea, though. – CJBS – 2018-01-06T07:30:20.503