Quick Tip: Re-Run The Last Command

Sometimes, while you’re poking around in the console, you want to re-run the last command. Sure, you can hit the up arrow and enter, but PowerShell always gives you multiple ways to do things.

It’s easy, using the Invoke-History cmdlet. You can also use its alias which is just r. Running that cmdlet without any parameters will run the last command from the console. Alternatively, you can specify a value for the ID parameter, and run one from further back in your history.

How do you find out what those IDs should be? Using Get-History to see everything you’ve run in this session, and the ID number for that command.

2 thoughts on “Quick Tip: Re-Run The Last Command

  1. You can also do a recursive search back through your history with Ctrl+r. I feel this is by far the most effectient way of re-running a previous command. This is a carry over along with others like Alt+. from Bash.

    1. I believe the Ctrl+R is a (default) binding of PSReadline and not PowerShell in general.

      The “remove from history” feature (and it is a feature) is only present in the PowerShell History, and not in the PSReadline History — the two are separate but usually track together, though clearing one is not clearing the other, nor is loading one with additional items (e.g., saved history) as loading the other.

      Ctrl+R is usually bound to ReverseSearchHistory, while most many people bind Up Arrow to HistorySearchBackwards

      PreviousHistory is pretty worthless (to me).

      But ReveseSearchHistory –RSH — (ctrl-R) is quite useful, as is HistorySearchBackwards (HSB).

      ReverseSearchHistory find ANY word, while HistorySearchBackwards matches the front of the line. Mostly the RSH is good for “things long ago” (maybe yesterday or earlier) while HSB tends to be best for recent commands though that is just a rough estimate.

      I use them both actively.

Leave a Reply

Your email address will not be published. Required fields are marked *