1

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 3

4

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
Sign up to request clarification or add additional context in comments.

2 Comments

Hello @DanielSmith your answer it's very good
Hum, I thought -count takes X from beginning, not from end. Had no time to run stupid update-help on this PC.
2

Get-History | select -last 1 | select -expand commandline

Comments

0

There is a commandlet for that, Invoke-History.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.