Weekend Scripter: Use PowerShell to Change Computer Icon Caption to Computer Name

Doctor Scripto

Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to change the caption of the computer icon to local computer name. 

Changing desktop icon captions

Microsoft Scripting Guy, Ed Wilson, is here. The Charlotte Windows PowerShell User Group this past Thursday was a lot of fun. We decided to hold a mini Scripting Games. Brian thought up the event, and he also served as one of the judges (I was the other judge). The attendees had 45 minutes to develop their answers, and then they stood in front of the group and explained their script. Brian and I then provided our grades and our critiques. The critiques included what we liked and suggestions for further improvement. The event seemed more like “Scripting with the Stars” instead of the actual Scripting Games; but it was so much fun, and we decided to do it again for our meeting in July.

The Scripting Wife and I left directly from the Windows PowerShell User Group meeting and headed to Pensacola, Florida for SQL Saturday. I decided to let the Scripting Wife drive, and so I could spend time playing around with Windows PowerShell on my laptop. Sometimes when I am bored, I like to explore the Shell.Application object. This COM object is really cool, and it contains a ton of functionality. I decided it would be fun to change the caption of my Computer Icon on my desktop to display the name of my laptop instead of computer / my computer.

Use the Shell.Application object to access computer icon

Almost exactly two years ago, on June 8, 2010, I wrote a Hey, Scripting Guy! Blog in which I obtained a list of all the shell namespaces, the numeric value associated with that namespace, and the path to that namespace. The script takes a while to run; and therefore, it writes to a text file. The file contains over a thousand lines of data, and it is shown here.

Image of command output

This file tells me that the computer namespace is 17. I use a variable named $My_Computer to hold the number 17. This is not a requirement, but I like to do this because it avoids supplying a completely meaningless number to the Namespace method from the Shell.Application object. This value assignment is shown here.

$My_Computer = 17

I then create an instance of the Shell.Application COM object. To do this, I use the New-Object cmdlet, and use the ComObject parameter to supply the name of the object to create. One thing I like is that I do not need to place the object name inside a pair of quotation marks. I store the returning Shell.Application object in a variable that I call $shell. The following line of code accomplishes this task.

$Shell = new-object -comobject shell.application

Note   The other day, I had a discussion with a person who used to write a lot of VBScript code. This person is learning Windows PowerShell and was still using the Hungarian Notation for naming variables. In Hungarian Notation, you use a prefix like obj when the variable will contain an object. I pointed out (in a slightly facetious manner) that because everything is an object in Windows PowerShell, he would be using the obj prefix an awfully lot. In general, most Windows PowerShell scripters tend to avoid Hungarian Notation for their variable naming convention.

I now use the Shell.Application object that I stored in the $shell variable, and I call the Namespace method to obtain the computer object. I store the computer namespace in a variable that I call $NSComputer. This task is shown in the following line of code.

$NSComputer = $Shell.Namespace($My_Computer)

The last task is to set the Name property on the computer icon. To do this, I use the Self property from the computer namespace object that is stored in the $NSComputer variable. When I have the object returned by the Self property, I assign the new value to the Name property of that object. To obtain the name of my computer, I use the ComputerName environmental variable. The line of code that does this is shown here.

$NSComputer.self.name = $env:COMPUTERNAME

I called the four-line script, ChangeComputerIconName.ps1. The complete script is shown here.


$My_Computer = 17

$Shell = new-object -comobject shell.application

$NSComputer = $Shell.Namespace($My_Computer)

$NSComputer.self.name = $env:COMPUTERNAME

The newly renamed computer icon now bears the name of my laptop. It is in the caption under the icon and it is shown here.

Image of icon

Well, it looks like traffic is becoming a bit heavy, and I am going to shut my laptop down for a while and help the Scripting Wife be on the lookout for squirrels.

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 

1 comment

Discussion is closed. Login to edit/delete existing comments.

  • J S 0

    Is there a way to make the icon appear on the desktop through com instead of regedit?

Feedback usabilla icon