{"id":3413,"date":"2013-06-12T00:01:00","date_gmt":"2013-06-12T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/06\/12\/translate-vbscript-into-powershellnot\/"},"modified":"2013-06-12T00:01:00","modified_gmt":"2013-06-12T00:01:00","slug":"translate-vbscript-into-powershellnot","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/translate-vbscript-into-powershellnot\/","title":{"rendered":"Translate VBScript into PowerShell&#8212;Not!"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary<\/strong><span style=\"font-size: 12px\">: Microsoft Scripting Guy, Ed Wilson, talks about translating VBScript script into Windows PowerShell, and he says it is not a very good idea.<\/span><\/p>\n<p><span style=\"font-size: 12px\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy! I love VBScript. I know, I know. It is not fashionable these days to say that, but it is a fact. I love using VBScript. It is powerful, and over the last ten years or so, I have written literally hundreds of scripts that we use at my company on a daily basis. I simply do not have time to translate these scripts in Windows PowerShell. I am sorry, but that is all there is to it&hellip;just sayin&rsquo;.<\/span><\/p>\n<p>&mdash;ML<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello ML,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife and I are going through what could be called TechEd withdrawl. After nearly a week of non-stop activity, literally from 6:00 AM to midnight each day, we find ourselves missing the fun, engagement, and excitement&mdash;like a rabbit locked out of a vegetable garden. It was too much, and now it is too little. Luckily, in a few weeks there is the <a href=\"http:\/\/www.atltechstravaganza.com\/\" target=\"_blank\">TechStravaganza 2013 in Atlanta<\/a>, and then there is <a href=\"http:\/\/europe.msteched.com\/#fbid=Y6s0qEgGIo3\" target=\"_blank\">TechEd 2013 Europe in Madrid<\/a>. So we get a chance to do it all over again.<\/p>\n<p>Anyway, ML, I thought I would take some time to review some of my email sent to <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, and I came across your letter. Here are some thoughts&hellip;<\/p>\n<h2>Do not translate VBScript into Windows PowerShell&hellip;<\/h2>\n<p>Never. Never. Never. Never. Never!<\/p>\n<p>Of the more than 3,000 VBScript scripts I wrote years ago, I have translated fewer than two dozen into Windows PowerShell. Each of those was for academic purposes, not for real-life purposes. The reason is that Windows PowerShell and VBScript are completely different. You may as well try to turn applesauce into mashed potatoes. It might seem like a good idea at the time&mdash;but dude! (or dudette!), I am not certain of a really acceptable outcome.<\/p>\n<p>Need some proof? OK, how about this one:<\/p>\n<p>This script is a basic WMI script from the Scripting Guys Script Center Repository. It is called <a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/ac1a95ef-96db-4706-845a-89e818a4a04f\" target=\"_blank\">Retrieving BIOS Information<\/a>, and it does a great job. In fact, I have used this particular script on more than one occasion over the last decade and a half&mdash;in production and in teaching situations. Here is the script:<\/p>\n<p style=\"padding-left: 30px\">strComputer = &#8220;.&#8221;<\/p>\n<p style=\"padding-left: 30px\">Set objWMIService = GetObject(&#8220;winmgmts:&#8221; _<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; &amp; &#8220;{impersonationLevel=impersonate}!\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Set colBIOS = objWMIService.ExecQuery _<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; (&#8220;Select * from Win32_BIOS&#8221;)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">For each objBIOS in colBIOS<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Wscript.Echo &#8220;Build Number: &#8221; &amp; objBIOS.BuildNumber<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Wscript.Echo &#8220;Current Language: &#8221; &amp; objBIOS.CurrentLanguage<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Wscript.Echo &#8220;Installable Languages: &#8221; &amp; objBIOS.InstallableLanguages<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Wscript.Echo &#8220;Manufacturer: &#8221; &amp; objBIOS.Manufacturer<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Wscript.Echo &#8220;Name: &#8221; &amp; objBIOS.Name<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Wscript.Echo &#8220;Primary BIOS: &#8221; &amp; objBIOS.PrimaryBIOS<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Wscript.Echo &#8220;Release Date: &#8221; &amp; objBIOS.ReleaseDate<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Wscript.Echo &#8220;Serial Number: &#8221; &amp; objBIOS.SerialNumber<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Wscript.Echo &#8220;SMBIOS Version: &#8221; &amp; objBIOS.SMBIOSBIOSVersion<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Wscript.Echo &#8220;SMBIOS Major Version: &#8221; &amp; objBIOS.SMBIOSMajorVersion<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Wscript.Echo &#8220;SMBIOS Minor Version: &#8221; &amp; objBIOS.SMBIOSMinorVersion<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Wscript.Echo &#8220;SMBIOS Present: &#8221; &amp; objBIOS.SMBIOSPresent<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Wscript.Echo &#8220;Status: &#8221; &amp; objBIOS.Status<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Wscript.Echo &#8220;Version: &#8221; &amp; objBIOS.Version<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; For i = 0 to Ubound(objBIOS.BiosCharacteristics)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Wscript.Echo &#8220;BIOS Characteristics: &#8221; &amp; _<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; objBIOS.BiosCharacteristics(i)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Next<\/p>\n<p style=\"padding-left: 30px\">Next<\/p>\n<h2>Translating to Windows PowerShell&hellip;<\/h2>\n<p>So we have a great VBScript script that retrieves bios information from local and remote computers, and it is something we use all the time. So we decide to translate it to Windows PowerShell. Here is the result:<\/p>\n<p style=\"padding-left: 30px\">$strComputer = &#8220;.&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">$colItems = get-wmiobject -class &#8220;Win32_BIOS&#8221; -namespace &#8220;root\\CIMV2&#8221; `<\/p>\n<p style=\"padding-left: 30px\">-computername $strComputer<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">foreach ($objItem in $colItems) {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;BIOS Characteristics: &#8221; $objItem.BiosCharacteristics<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;BIOS Version: &#8221; $objItem.BIOSVersion<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Build Number: &#8221; $objItem.BuildNumber<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Caption: &#8221; $objItem.Caption<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Code Set: &#8221; $objItem.CodeSet<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Current Language: &#8221; $objItem.CurrentLanguage<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Description: &#8221; $objItem.Description<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Identification Code: &#8221; $objItem.IdentificationCode<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Installable Languages: &#8221; $objItem.InstallableLanguages<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Installation Date: &#8221; $objItem.InstallDate<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Language Edition: &#8221; $objItem.LanguageEdition<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;List Of Languages: &#8221; $objItem.ListOfLanguages<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Manufacturer: &#8221; $objItem.Manufacturer<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Name: &#8221; $objItem.Name<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Other Target Operating System: &#8221; $objItem.OtherTargetOS<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Primary BIOS: &#8221; $objItem.PrimaryBIOS<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Release Date: &#8221; $objItem.ReleaseDate<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Serial Number: &#8221; $objItem.SerialNumber<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;SMBIOS BIOS Version: &#8221; $objItem.SMBIOSBIOSVersion<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;SMBIOS Major Version: &#8221; $objItem.SMBIOSMajorVersion<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;SMBIOS Minor Version: &#8221; $objItem.SMBIOSMinorVersion<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;SMBIOS Present: &#8221; $objItem.SMBIOSPresent<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Software Element ID: &#8221; $objItem.SoftwareElementID<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Software Element State: &#8221; $objItem.SoftwareElementState<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Status: &#8221; $objItem.Status<\/p>\n<p style=\"padding-left: 30px\">&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;write-host &#8220;Target Operating System: &#8221; $objItem.TargetOperatingSystem<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Version: &#8221; $objItem.Version<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p>This is an actual script that comes from the Script Center Repository: <a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/acf471f1-9e8c-4296-acef-a6d6871e2e90\" target=\"_blank\">List BIOS Information<\/a>. I am including the link because it is a perfect example of a horrible Windows PowerShell script, and anyone teaching a Windows PowerShell class should have access to a script such as this. (And no, I did not write this script&mdash;it was bulk uploaded four years ago using the Scripting Guys Live ID&hellip;Oh well.)<\/p>\n<p>Comparing the two scripts, I see very little difference. Here are the two scripts (somewhat truncated) side-by-side:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2210.hsg-6-12-13-1.png\"><img decoding=\"async\" title=\"Image of scripts\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2210.hsg-6-12-13-1.png\" alt=\"Image of scripts\" \/><\/a><\/p>\n<h2>Writing the script in Windows PowerShell&hellip;<\/h2>\n<p>Instead of translating VBScript into Windows PowerShell, take advantage of the inherent capabilities of Windows PowerShell instead of forcing Windows PowerShell to do VBScript. Indeed, it is not my intention to make fun of VBScript, because I have also seen Windows PowerShell code that was written in C#, or C++, or Perl.<\/p>\n<p>The cool thing is that Windows PowerShell is very flexible, and it is possible to get things up and running easily. Here is the previous BIOS script, written so that it takes advantage of Windows PowerShell:<\/p>\n<p style=\"padding-left: 30px\">Get-WmiObject Win32_Bios<\/p>\n<p>That is it.<\/p>\n<p>This connects to WMI and retrieves BIOS information. It can also be written by using the CIM cmdlets from Windows PowerShell 3.0 as shown here:<\/p>\n<p style=\"padding-left: 30px\">Get-CimInstance Win32_Bios<\/p>\n<p>The thing that is incredible in both examples is that there are fewer LETTERS in the command than there are LINES in the previous script.<\/p>\n<p>So, ML, please, please, please, please do not translate your old VBScript scripts into Windows PowerShell. If you do, you will do the following:<\/p>\n<ol>\n<li>Waste your time redoing something that already works.<\/li>\n<li>Waste your time by writing very bad Windows PowerShell code.<\/li>\n<li>Come away confused as to what &ldquo;all the fuss&rdquo; surrounding Windows PowerShell is about.<\/li>\n<li>Contribute to the already too large pool of bad Windows PowerShell code.<\/li>\n<\/ol>\n<p>ML, your job as an IT pro is to get stuff done, not to write code (Windows PowerShell, VBScript, or otherwise). Therefore, if you have something that works, stay with it.<\/p>\n<p>If or when you need NEW code, I would advise you to write it in Windows PowerShell. To get started, see the <a href=\"http:\/\/technet.microsoft.com\/en-us\/scriptcenter\/dd742419.aspx\" target=\"_blank\">Scripting with Windows PowerShell<\/a> in the Script Center. You might also want to pick up a copy of my book, <a href=\"http:\/\/www.amazon.com\/Windows-PowerShell-3-0-Step\/dp\/0735663394\/ref=sr_1_1?ie=UTF8&amp;qid=1344869046&amp;sr=8-1&amp;keywords=powershell%203.0\" target=\"_blank\">Windows PowerShell 3.0 Step by Step<\/a>, and follow along there.<\/p>\n<p>ML, there are my thoughts about translating VBScript to Windows PowerShell. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.<\/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><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><span style=\"font-size: 12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about translating VBScript script into Windows PowerShell, and he says it is not a very good idea. &nbsp;Hey, Scripting Guy! I love VBScript. I know, I know. It is not fashionable these days to say that, but it is a fact. I love using VBScript. It is powerful, [&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":[51,3,4,45],"class_list":["post-3413","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about translating VBScript script into Windows PowerShell, and he says it is not a very good idea. &nbsp;Hey, Scripting Guy! I love VBScript. I know, I know. It is not fashionable these days to say that, but it is a fact. I love using VBScript. It is powerful, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3413","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=3413"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3413\/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=3413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}