PowerShell – How to view command history?

Whenever you enter commands at PowerShell command prompt, internally PowerShell maintains session history; the commands we entered during the current session. We can retrieve these commands through PowerShell cmdlets. We are going to discuss these commands through this Article.

The Get-History command

This command is used to display the command history information which is stored during the current session. By default, it shows all the commands ran during the session and it display the ID which represents the order in which the commands ran.

PS C:\> Get-History

  Id CommandLine
  -- -----------
   1 cd \
   2 cls
   5 $var = dir
   6 Get-History
   6 clear

That’s it? That’s so simple? Yes. It’s so simple. You can combine this command with other commands too.

The Clear-History command

This command clears all the commands in the current session.

PS:\> Clear-History

The Invoke-History command

PowerShell allows you to run the previously ran commands from the session’s history. It takes arguments and if you do not pass any arguments, it invokes the latest command in the session’s history.

You can specify particular command to execute by specifying the ID of the command from the session’s history. You can come to know the ID of the command through Get-History command. Here is an example;

PS:\> Invoke-History -ID 2

It runs the command which has the ID 2, from the session’s history.

[..] David

PowerShell – How to view command history?

One thought on “PowerShell – How to view command history?

  1. Pingback: URL

Leave a Reply

Your email address will not be published.

Scroll to top