Mr. Script

Give the My Computer Icon a User Name

It is handy to be able to just look up at the My Computer icon and see where you are and what name you are using.

>Last month, we created a script that used Windows Management Instrumentation (WMI) to manage Registry permissions. While it didn’t allow changing those permissions (a limitation of the WMI Standard Registry provider—StdRegProv), it did provide useful functionality to determine the rights of the current user. Furthermore, it got me thinking about how often I find myself digging into the Registry and how limited the “standard” scripting tools are at providing this capability.

I’ve covered the topic of editing the Registry via scripting a few times over the years and have always relied on one of two methods:

 The WshShell Object—In addition to managing special folders, logging events, creating shortcuts and other assorted responsibilities, the WshShell object has two methods for managing the registry: RegRead and RegWrite. While it’s fast and easy to use, it isn’t well suited for anything but the simplest Registry editing tasks.

 The WMI StdRegProv—WMI is virtually god-like in its capabilities. Unfortunately, it’s quite complex and can be daunting to anyone who hasn’t devoted at least six weeks to learning its extensive object model. The WMI Object Browser doesn’t make things any easier, either.

You’re probably thinking, “What’s a burgeoning scripter supposed to do? Is there no middle ground?” Indeed, there is!

Component-Based Registry Scripting
The Registry Object (RegObj.dll) is an ActiveX component created by Microsoft that allows you to manipulate the Registry from within your scripts in a straightforward but flexible manner. It blows away the WshShell object in terms of capability and is much easier to use than WMI. In fact, about the only Registry editing function WMI can perform that RegObj can’t is viewing permissions.

RegObj.dll
RegObj.dll has objects for each part of the Registry, including constants. (Click image to view larger version.)

Before you can write a script with this little gem, you need to download it. One place to get the component is from http://cwashington.netreach.net. It’s packaged as registry_object.zip, and it needs to be extracted to your system32 directory and registered:

C:\Windows\system32>regsvr32 regobj.dll

Once the component is registered, you can access it from within your scripts—just like any other component—either by using the CreateObject statement in the script code or by referencing it using the tag in the XML portion of your .WSF scripts. I recommend the latter, for reasons that will become apparent. You can view the RegObj type library from within XRay.exe (my favorite free type library viewer). Load the component into XRay (or your preferred object browser) and take a minute to get familiar with the RegObj object model.

Now that you’ve downloaded, installed and registered this component, let’s get right into using it.



MyCompChangeName.wsf
This script changes the text displayed under the 'My Computer'
icon from "My Computer" to the name of the currently logged on user, the computer or both.


 
  
   

   Script changing My Computer to current user or computer
   

   
   C:\>cscript MyCompChangeName.wsf /OS:XP /Text:B
   

   <>
   name="
OS"
   helpstring="'NT', '2K', or 'XP'"
   type="string"
   required="
true"
   />    

   <>
   name="
Text"
   helpstring="
'U' for username; 'C' for computername; 'B' for     both"

   type="string"
   required="
true"
   />
  
  " progid="
regobj.registry" reference/>
  
  

What’s Your Name?
This script performs one of my favorite Registry tweaks: changing the name displayed under the “My Computer” icon on the desktop (and inside Windows Explorer) to the name of the user who is currently logged on, the computer name or both (displaying just the user name is the most useful for me). How this is accomplished depends upon which version of Windows you’re running (see the “References” sidebar in the online version of this article), but this script can be quite useful! As administrators, we often go from server to desktop to server logging in and out under a variety of names. Sometimes we log on as ourselves, sometimes as the local administrator, and sometimes under the domain admin account. It’s handy to be able to just look up at the My Computer icon and see where you are and what name you’re using.

References
The good news is that, regardless of which version of Windows you use, the “My Computer” text information is stored in the same Registry key (that long CLSID key listed in the script). This simplifies things somewhat. Under NT 4.0, Ref1, the text is stored in the default value of this key. To use an environment variable in the Registry, the value type must be REG_EXPAND_SZ (a variable length string). Alas, the current value type is set as REG_SZ (a fixed length string) because the length of the string—”My Computer”—doesn’t change! With Regedit, once a value has been created, you can’t change the value type. You can only delete the old value and create a new one of the appropriate type. RegObj allows you to change the value type of the default value to REG_EXPAND_SZ. You can then change the data to display the appropriate text.

With Windows 2000, Ref2, this text is stored in the “LocalizedString” value (also having the value type REG_SZ). You don’t want to change the entire string, just the last bit, so you put the data from this value into a temporary string and replace the last 11 characters (“My Computer”) with the appropriate text, and change the type to REG_EXPAND_SZ.

Windows XP, Ref3, uses the same value as Win2K and is ahead of the game in that the value type is already set as REG_EXPAND_SZ. However, with XP, you delete all the old data in the value and replace it only with the appropriate environment variables. In order to revert to the standard “My Computer” at a later date, you must save the existing data in a separate value. (With NT 4.0 and Win2K, all you have to do is change the appropriate value back to “My Computer” and change the value type back to REG_SZ.) In this case, you just create a new value called “LocalizedString.old” to store the original data.

—Chris Brooke

Always Check Your References
If you think back to last month’s script, the first thing I did was set up constants for both the root keys and permissions that mapped to the appropriate hex values used by WMI. I only set up the ones I was using in the script, so it was no big deal. However, if I were building a script to have any degree of flexibility, I’d be required to set up all those values as constants. A slightly bigger deal, no?

Remember earlier when I said that I recommend using the tag to reference the RegObj component? Well, this is so important because it allows me to use the reference attribute in the object declaration. This gives me access to all the constants included in the RegObj component. Bottom line: You don’t have to declare a bunch of constants. You can reference the constants in the type library directly by name and be assured they hold the correct values.

This script still has some areas where it can be improved. First, if the data in the key doesn’t match the expected default values, the script quits without making any changes. Second, it performs no verification that the OS specified in the argument is, indeed, the correct OS. See what you can do to correct these issues.

comments powered by Disqus

Reader Comments:

Tue, Dec 13, 2011 Jeff NY

Actually, it's nice to have this set up simply because you have idiot users who change their computer name, then you have to take them through right-clicking or opening a command line (not easy on the phone, a lot of the time, depending on the user) to get the information. So to have it set as a policy - The name of "My Computer" MUST BE "My Computer - %hostname%" - is very helpful.

Tue, Dec 30, 2003 Oscar SA

As an example of how to use the Register Object Active X component, this is great. It gets the juices flowing in the brain and gives me new ideas of how I could use this Active X component for other things. Problem is, as a newbie I have enough problems trying to follow the code, without have to make corrections (mainly because I don’t know what corrections to make). Is there updated code somewhere or does someone have copy of the code with corrections?

Tue, Dec 2, 2003 Ron Kansas

Holy crap you guys are morons... He's not showing ONLY how to Rename the My Computer icon... That's an EXAMPLE of scripting against the registry. WMI works for fine for me, so I won't be using this method, but EVERY decent admin should know how to script against remote registries and he's showing you another way to do it.

Sun, Aug 10, 2003 Anonymous spain

lol true...rename takes like what...2 seconds? rotfl

Wed, Aug 6, 2003 Anonymous Anonymous

Hmmm those folks in LA dont seem to know that more that one person can log on to a server.

Wed, Jul 23, 2003 Anonymous Anonymous

Anonymous from NO, your trick is easy if you only have a dozen computers. Now, try doing that same trick at a company with more than 200 computers and do your regular job. DUH! Chris' script starts to look good now, huh?

Wed, Jul 16, 2003 Anonymous Anonymous

I'm going to start ignoring any articles by Chris Brooke. Nothing worse than a newbie teaching newbies bad coding habits.

Wed, Jul 16, 2003 Stefan Schäfer Frankfurt

I had to change the script code because I received several error messages.
Finally I got this script to do his job and it works fine (OS: WinXP).

Mon, Jul 14, 2003 Anonymous N.O. LA

how about right clicking and choosing rename?!.....DUH!!!!!!!

Mon, Jul 14, 2003 Eric New Oleans

Very weak, actually.....

Fri, Jul 11, 2003 Mike Tampa

That was pretty Weak.

Mon, Jul 7, 2003 Anonymous Anonymous

how about something that hasn't already been done better about a billion times already?

Wed, Jul 2, 2003 Christopher D. Bell Manchester

ctrl-alt-del works for me and requires NO waste of time!

Mon, Jun 30, 2003 Anonymous Anonymous

kewl

Fri, Jun 27, 2003 Tom Sapp Arizona

I finally got the script to run on xp but if ran on 2k i get and error message of "Microsoft VBScript runtime error: Object doesn't support this property or method: 'WScript.Arguments.Named'" and it will not make the necessary changes.

Add Your Comment Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Comment:
Please type the letters/numbers you see above

Redmond Tech Watch

Sign up for our newsletter.

I agree to this site's Privacy Policy.