≡ Menu

PowerShell: Execute a command from history

PowerShell has facility where in your can view the commands executed in the current shell. You can use Get-History cmdlet to see all the previously executed commands. Note that PowerShell V3 & above will show all commands you executed while the PowerShell V2 shows only last 32 commands you executed.

Now the question is how do we execute a command from list returned by Get-history. One way is to copy paste the commands manually but that is not efficient. PowerShell has some near way of doing it. Let us see how to do that.

To access any item in history by using ID, you can try the below command. This command will fetch the item from history which has ID number 3.

PS C:\>#3 <tab>

Similar you can fetch the commands from history by using keywords as well. For example, below command will get commands from history that has given keyword anywhere in the command. If multiple commands are there from history that matches the keyword, you can keep pressing the tab till you reach the one you need in history.

PS C:\>#keyword <tab>

Looks very easy and efficient, Correct? Let us jump into some practices now. This video will demonstrate how to use it.

Happy learning…