I'm looking for a command to get the last executed command as output. So far I found the automatic variable $$, but it gives only the last word of the last executed command. I'd like to avoid to use the up arrow of the keyboard (I need a command, not a key).
3 Answers
If the last command was part of your current session (vs. the last command at the end of your previous session), you can use the Get-History cmdlet:
Get-History -Count 1
To get just the text of the command, you can pipe it through Select-Object:
Get-History -Count 1 | Select-Object -ExpandProperty CommandLine
2 Comments
Gennaro Arguzzi
Hello @DanielSmith your answer it's very good
Vesper
Hum, I thought
-count takes X from beginning, not from end. Had no time to run stupid update-help on this PC.