Hey, Scripting Guy! How can I determine the default buffer size for the command window?
Hey, GO. You know, some of you might be wondering, “Why a daily scripting column? Why not, say, a daily scripting TV show?” That’s a good question, and there are a number of reasons for that. For one thing, there’s the fact that even Microsoft couldn’t afford the makeup required to get the Scripting Guys ready to appear on TV each day. Perhaps more important, however, is the fact that, to be on live TV, you need to be the kind of person who doesn’t make many mistakes. If you’re not, you end up looking silly. Needless to say, the Scripting Guys would never do anything that might compromise their dignity and sense of professionalism.
Where did all this come from? Well, the other day the Scripting Guy who writes this column was watching basketball on TV when the announcer mentioned someone who had “recently suffered a fatal death.” Admittedly, the Scripting Guy who writes this column is no doctor (something the rest of the world is eternally grateful for). Nevertheless, that left him wondering: if you’re going to suffer death, would it be better to suffer a non-fatal death? A fatal death sounds far more serious.
Not to mention the fact that it makes the speaker sound far sillier.
At any rate, that’s why the Scripting Guys don’t do live TV: on live TV there’s no Undo command enabling them to erase their mistakes. For example, when putting together the script for today’s column the Scripting Guy who writes this column mistyped the command Wscript.Echo. But you’d never know that by looking at the finished version, would you:
HKEY_CURRENT_USER = &H80000001 strComputer = "." Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv") strKeyPath = "Console" strValueName = "HistoryBufferSize" objRegistry.GetDWORDValue HKEY_CURRENT_USER, strKeyPath, strValueName, dwValue Wscript.Echo dwValue
As it turns out, GO, the default buffer size for the command window (along with a number of other default command window settings) is stored in the registry; to get at this information all we have to do is read the appropriate registry value.
So let’s go ahead and do just that. To begin with, we define a constant named HKEY_CURRENT_USER and set the value to &H80000001; this tells the script which registry hive we want to work with. We then connect to the WMI service on the local computer. And yes, as is usually the case with WMI scripts, we could just as easily perform this task on a remote computer; all we have to do is assign the name of that computer to the variable strComputer:
strComputer = "atl-fs-01"
So far so good, right? After connecting to the WMI service (note that we connect to the root\Default namespace, rather than the root\cimv2 namespace) we then assign values to a pair of variables, strKeyPath and strValueName:
strKeyPath = "Console" strValueName = "HistoryBufferSize"
We’ll use strKeyPath to indicate the registry path within the HKEY_CURRENT_USER hive; because the registry value we want to read resides in HKEY_CURRENT_USER\Console we assign the value Console to strKeyPath. Meanwhile, we assign the name of the registry value we’re interested in to the variable strValueName. Because we want to read the registry value named HistoryBufferSize we – that’s right, we assign HistoryBufferSize to strValueName.
At this point we’re ready to go ahead and retrieve the value from the registry, something we do using the GetDWORDValue method:objRegistry.GetDWORDValue HKEY_CURRENT_USER, strKeyPath, strValueName, dwValue
As you can see, we simply call GetDWORDValue, passing four parameters: the constant HKEY_CURRENT_USER; the variables strKeyPath and strValueName; and an “out” parameter we named dwValue. An out parameter is simply a variable (of our choosing) that the method will assign a value; in this case that means GetDWORDValue will read HistoryBufferSize and assign that value to dwValue. Once that’s been done all we have to do is echo back the value of dwValue:Wscript.Echo dwValue
That’s all we have to do.
By default the command history buffer size is set to 50; however, you can modify the buffer size, giving it a value between 1 and 999. How can you modify the buffer size? Well, here’s one way to do it:HKEY_CURRENT_USER = &H80000001 strComputer = "." Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv") strKeyPath = "Console" strValueName = "HistoryBufferSize" dwValue = 250 objRegistry.SetDWORDValue HKEY_CURRENT_USER, strKeyPath, strValueName, dwValue
We won’t discuss this bonus script in any detail, but this little bit of code sets the history buffer size to 250. Would you prefer some other history buffer size? No problem; just change this line accordingly:dwValue = 250
And there you have it.
By the way, we know that a number of you are already busy composing emails pointing out that, even though this isn’t a live TV show, the Hey, Scripting Guy! column is often riddled with statements that make no sense whatsoever. We’d just like to take this opportunity to note that any silliness or nonsense that might appear in this column is solely the responsibility of the Scripting Editor. We apologize for any inconvenience she might have caused you.