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

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

  • Comments 3
  • Likes

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.

ChangeComputerIconName.ps1

$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 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • One caveat to this script is that if the name of the computer changes, the icon will not be updated unless the script is run again. This can be accomplished as a logon script, which affords more possibilities such as adding the logged on username to the icon as well:  

    $NSComputer.self.name = "{0} on {1}" -f $env:USERNAME,$env:COMPUTERNAME

    Note: Setting the My Computer icon in XP/Vista as a variable value (%USERNAME% on %COMPUTERNAME%) was possible, but permissions have changed in W7 so that not even administrators have access to the registry key required without taking ownership of it.

  • Wow! A blog with a menu.  It's becoming a portal.

    Keep it up Ed.  Looks good.

  • These both work well on Windows 7.

    (new-object -comobject shell.application).Namespace(17).self.name=$env:COMPUTERNAME

    (new-object -comobject shell.application).Namespace(17).self.name=("{0}`n{1}" -f $env:userdomain,$env:username)

    I have long used the Computer 'icon' label to tell me which account I am currently logged into.  I generally run as non-admin and lose track of which session is admin and which non-admin.  I do not like to go web hopping as admin.  Of course Windows 7 does make this much safer than in XP or WS2003.  I just dodged a very nasty Trojan with WS2003 because I was not an admin.  If I had been the system would have been compromised.  I clicked a Google search link and all hell busted out.  I had to log off.  I couldn't exit IE and had to kill it.  Meanwhile TCPView showed hundreds of connections being made.  Knowing that I was non-admin allowed me to be less concerned as checking the Trojan info showed that it was mot likely to infect a low level account permanently.

    BGInfo is another useful tools.

    Ed - do you know how to ge3nerate a tooltip for the computer icon?  I used to know but haven’t used it for quite a while.  We used to set it up to be a dynamic display of all user and computer info for the desktop support guys to use.  It was less obtrusive than BGInfo.