Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

Increase PowerShell Command History to the Max (Almost)

$
0
0

Summary: In Windows PowerShell command history, you may run into the console history limit.

Microsoft Scripting Guy, Ed Wilson, is here. While I was in Cincinnati, Ohio for their first Windows PowerShell User Group meeting, I had a chance to meet a lot of my old friends. Later that week when I was teaching a Windows PowerShell workshop for a customer, I had a chance to meet other old friends. You might say that I was geeking out on seeing so many of my old friends—in fact, I was a bit worried that I would overflow my personal history buffer. Luckily, in Windows PowerShell 3.0, overflowing the history buffer is a bit difficult to do.

Playing around with Windows PowerShell 3.0 history settings

In Windows PowerShell 3.0 the MaximumHistoryCount is set to 4096. I find this value by using the Get-Variable cmdlet. This command is shown here.

PS C:\> Get-Variable -Name MaximumHistoryCount

Name                           Value

----                           -----

MaximumHistoryCount            4096

At 4096, it is unlikely that I will ever need to increase the storage space. In Windows PowerShell 1.0, it was set to 64, and I frequently was overwriting my command history. I generally do not keep the same Windows PowerShell console session open long enough to type 4096 commands—so I have never overwritten that history buffer. But what if I feel the need to increase my command history? What is the biggest value I can assign? One way to find out is to simply run a command and see what happens. Using one of my top ten favorite Windows PowerShell tricks, I use the range operator and begin assigning new values. Here is the command I use.

1..50000 | % {Set-Variable -Name MaximumHistoryCount -Value $_ }

What I am looking for is an error to raise that states I have exceeded the allowed value for the variable. This is exactly what happens, and I press CTRL-C to break the command and prevent further errors from appearing in the Windows PowerShell console. The command and the error that appears when the command runs are shown here.

Image of command output

A better approach to finding maximum values that are permitted for a variable involves using the Get-Variable cmdlet. In the following command, I return information about the MaximumHistoryCount variable by piping the output to the Format-List cmdlet.

PS C:\> Get-Variable MaximumHistoryCount | fl *

 

Name        : MaximumHistoryCount

Description : The maximum number of history objects to retain in a session.

Value       : 4096

Visibility  : Public

Module      :

ModuleName  :

Options     : None

Attributes  : {System.Management.Automation.ValidateRangeAttribute}

I see that the Attributes property contains an object. To investigate the Attributes property, I pipe the results of Get-Variable to the Select-Object cmdlet, and I use the ExpandProperty parameter. This technique is shown here.

PS C:\> Get-Variable MaximumHistoryCount | Select-Object -ExpandProperty attributes

 

                    MinRange                    MaxRange TypeId

                    --------                    -------- ------

1                                           32767 System.Management.Automa...

The MaxRange property tells me that the maximum allowed value for MaximumHistoryCount automatic variable is 32767. Interestingly enough, the error shown in the previous image occurs when I attempt to set the value to 32768, and I could assume that 32767 was the most that I could store. Now I know for certain.

Join me tomorrow when I will talk about more cool Windows PowerShell stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 


Viewing all articles
Browse latest Browse all 3333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>