Condividi tramite


Using PowerShell History ( Part 1 )

I like trying to figure out how things work and most of the time, it is via trial and error. Then I experiment to test my understanding and discovery a few new tricks along the way. Most of the time, this discovery happens while I am in the Windows PowerShell Console.

One of the things I rely on is the Get-History cmdlet. It can list out the last commands that have been typed in the console window.

For example, if you want to enumerate the last 50 commands, you can get them by typing:

   1: Get-History -c 50
   2: 

You can also use the history alias to perform the same thing command:

   1: history -c 50
   2: 

By default, the history buffer is limited to the last 64 commands. For most situations, this is adequate but sometimes, after experimenting on those fairly complex commands and trying different things, you could easily reach this limit very quickly. Fortunately, you can actually change this limit for the current PowerShell session by setting a value to the $MaximumHistoryCount variable.

To determine the current maximum history buffer, you can type:

   1: $MaximumHistoryCount
   2: 

Likewise, if you would like to raise the limit to 2000, you can set the variable value by:

   1: $MaximumHistoryCount = 2000
   2: 

Now, you can retrieve the last 2000 commands that you’ve been working hard on and have it within reach….until of course you hit that new limit. Do remember that the setting is only good for that current PowerShell session.