{"id":9171,"date":"2012-06-09T00:01:00","date_gmt":"2012-06-09T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/06\/09\/weekend-scripter-use-powershell-to-change-computer-icon-caption-to-computer-name\/"},"modified":"2012-06-09T00:01:00","modified_gmt":"2012-06-09T00:01:00","slug":"weekend-scripter-use-powershell-to-change-computer-icon-caption-to-computer-name","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-use-powershell-to-change-computer-icon-caption-to-computer-name\/","title":{"rendered":"Weekend Scripter: Use PowerShell to Change Computer Icon Caption to Computer Name"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to change the caption of the computer icon to local computer name.&nbsp;<\/p>\n<h2>Changing desktop icon captions<\/h2>\n<p>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 &ldquo;Scripting with the Stars&rdquo; instead of the actual Scripting Games; but it was so much fun, and we decided to do it again for our meeting in July.<\/p>\n<p>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 <b>computer \/ my computer<\/b>.<\/p>\n<h2>Use the Shell.Application object to access computer icon<\/h2>\n<p>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.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1212.wes-6-9-12-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1212.wes-6-9-12-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>This file tells me that the computer namespace is 17. I use a variable named <b>$My_Computer<\/b><i> <\/i>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 <b>Namespace<\/b><i> <\/i>method from the <b>Shell.Application<\/b> object. This value assignment is shown here.<\/p>\n<p style=\"padding-left: 30px\">$My_Computer = 17<\/p>\n<p>I then create an instance of the <b>Shell.Application COM<\/b> object. To do this, I use the <b>New-Object<\/b> cmdlet, and use the <i>ComObject <\/i>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 <b>Shell.Application<\/b> object in a variable that I call <b>$shell<\/b>. The following line of code accomplishes this task.<\/p>\n<p style=\"padding-left: 30px\">$Shell = new-object -comobject shell.application<\/p>\n<p style=\"padding-left: 30px\"><b>Note&nbsp;&nbsp;&nbsp;<\/b>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 <i>obj<\/i> 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 <i>obj<\/i> prefix an awfully lot. In general, most Windows PowerShell scripters tend to avoid Hungarian Notation for their variable naming convention.<\/p>\n<p>I now use the <b>Shell.Application<\/b> object that I stored in the <b>$shell<\/b> variable, and I call the <b>Namespace<\/b><i> <\/i>method to obtain the computer object. I store the computer namespace in a variable that I call <b>$NSComputer<\/b>. This task is shown in the following line of code.<\/p>\n<p style=\"padding-left: 30px\">$NSComputer = $Shell.Namespace($My_Computer)<\/p>\n<p>The last task is to set the <b>Name<\/b><i> <\/i>property on the computer icon. To do this, I use the <b>Self<\/b><i> <\/i>property from the computer namespace object that is stored in the <b>$NSComputer<\/b> variable. When I have the object returned by the <b>Self<\/b><i> <\/i>property, I assign the new value to the <b>Name<\/b><i> <\/i>property of that object. To obtain the name of my computer, I use the <b>ComputerName<\/b><i> <\/i>environmental variable. The line of code that does this is shown here.<\/p>\n<p style=\"padding-left: 30px\">$NSComputer.self.name = $env:COMPUTERNAME<\/p>\n<p>I called the four-line script, ChangeComputerIconName.ps1. The complete script is shown here.<\/p>\n<p style=\"padding-left: 30px\"><strong>ChangeComputerIconName.ps1<\/strong><\/p>\n<p style=\"padding-left: 30px\">$My_Computer = 17<\/p>\n<p style=\"padding-left: 30px\">$Shell = new-object -comobject shell.application<\/p>\n<p style=\"padding-left: 30px\">$NSComputer = $Shell.Namespace($My_Computer)<\/p>\n<p style=\"padding-left: 30px\">$NSComputer.self.name = $env:COMPUTERNAME<\/p>\n<p>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.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2570.wes-6-9-12-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2570.wes-6-9-12-02.png\" alt=\"Image of icon\" title=\"Image of icon\" \/><\/a><\/p>\n<p>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.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to change the caption of the computer icon to local computer name.&nbsp; 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 [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[16,340,3,94,61,226,45],"class_list":["post-9171","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-desktop-management","tag-general-mgmt-tasks","tag-scripting-guy","tag-special-folders","tag-weekend-scripter","tag-windows-explorer","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to change the caption of the computer icon to local computer name.&nbsp; 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/9171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=9171"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/9171\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=9171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=9171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=9171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}