Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Friday Fun Re-Run Last Command

Posted on July 29, 2011July 23, 2011

Ok, I'll be the first to admit you might not find a production use for this tip, but that's what makes it fun. Interactively, you can always hit the up arrow to get the last command in your command buffer. But what if you are running a script and for some reason want to re-rerun the last command? Using Get-History we can get a listing of all the commands. This cmdlet has an alias of 'h'. To re-run any command we use Invoke-History and specify the id number. Invoke-History has an alias of 'r'.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

[cc lang="PowerShell"]

PS S:\> r 163
get-service spooler

Status Name DisplayName
------ ---- -----------
Running spooler Print Spooler
[/cc]

For the programmatic approach, we need to find the last id number in history. Here's how:

[cc lang="PowerShell"]
PS S:\> (get-history)[-1].id
167
[/cc]

The Get-History expression returns an array. Using [-1] returns the last item in the array and then I'm only asking for the ID property. See where this is going? To run the last command we can pass this value to Invoke-History. We could do this, because the ID parameter accepts pipeline binding by property name:

[cc lang="PowerShell"]
(get-history)[-1] | invoke-history
[/cc]

Or like this (using aliases to shorten the command for an interactive session):

[cc lang="PowerShell"]
r (h)[-1]
[/cc]

Finally, we might want to turn this into a quicky function:

[cc lang="PowerShell"]
function rlast {
Invoke-History (Get-History)[-1]
}
[/cc]

Not life-changing, I know. But perhaps you'll pick up a tidbit or two about how PowerShell works.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

3 thoughts on “Friday Fun Re-Run Last Command”

  1. JV says:
    July 29, 2011 at 2:27 pm

    What’s wrong with teh old standby:
    & $^ $$
    Or
    .
    or

    or

    I think there are too many ways to do this.

    Have a good weekend Jeff.

  2. Greg Wojan says:
    August 23, 2011 at 10:49 am

    What’s wrong with using r? By default Invoke-History executes the last command if you don’t pass it a parameter.

    –Greg

    1. Jeffery Hicks says:
      August 23, 2011 at 11:21 am

      Absolutely nothing! This is one of those little gems that are so easy to overlook. It never crossed my mind to try Invoke-History without any parameters. Another great example of why you should read cmdlet help, even for cmdlets you *think* you know.

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d