{"id":3379,"date":"2013-06-19T00:01:00","date_gmt":"2013-06-19T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/06\/19\/dont-translate-file-and-folder-vbscript-scripts-into-powershell\/"},"modified":"2013-06-19T00:01:00","modified_gmt":"2013-06-19T00:01:00","slug":"dont-translate-file-and-folder-vbscript-scripts-into-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/dont-translate-file-and-folder-vbscript-scripts-into-powershell\/","title":{"rendered":"Don&#039;t Translate File and Folder VBScript Scripts into PowerShell"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary<\/strong><span style=\"font-size: 12px\">: Simplify your code and do not translate an old file and folder VBScript script into Windows PowerShell.<\/span>\n<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 know there are not really established patterns and practices for writing VBScript code, or even for writing Windows PowerShell script. But there is code that I like to use&mdash;over and over again. I am sending you a bit of code that I use anytime I need to find out if a folder exists. I am thinking of translating it to Windows PowerShell because I know that Windows PowerShell can work with COM objects, and I want to stay with things that I am familiar with. Is this a good idea or what?\n&mdash;KM\n<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 KM,\nMicrosoft Scripting Guy, Ed Wilson, is here. Today I am relaxing with a cup of Gunpowder green tea. I put a crushed cinnamon stick in the pot along with a bit of hibiscus flower, rose petal, and lemon grass. The tea is nearly perfect. Unfortunately, I am out of Anzac biscuits, so I am down to store-boxed Danish butter cookies. They are OK, but not really authentic.\nKM, speaking of things that are not really authentic&hellip;<\/p>\n<p style=\"padding-left: 30px\">Translating VBScript code directly into Windows PowerShell results in Windows PowerShell script that is not really authentic Windows PowerShell script. Let me provide you with a quick example.<\/p>\n<h2>Determine if a folder exists: the VBScript way<\/h2>\n<p>To determine if a folder exists by using VBScript, is not too bad of a task&mdash;especially if you have written the code a hundred times before. Then it will even seem familiar.<span style=\"font-size: 12px\">&nbsp;<\/span>\nKM, it is not a great idea to translate VBScript code directly into Windows PowerShell because you lose much of the Windows PowerShell advantage for the sake of only a little bit of familiarity and comfort that comes with the old code. The technique uses the <strong>FolderExists<\/strong> method from the <strong>FileSystemObject<\/strong>. (This script comes from the Script Center Repository.)\n<a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/fc32aeaa-df8e-4a78-b5ed-727465af8e60\" target=\"_blank\">Verify that a folder exists<\/a>\nVBScript<\/p>\n<p style=\"padding-left: 30px\">Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<\/p>\n<p style=\"padding-left: 30px\">If objFSO.FolderExists(&#8220;C:FSO&#8221;) Then<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Set objFolder = objFSO.GetFolder(&#8220;C:FSO&#8221;)<\/p>\n<p style=\"padding-left: 30px\">Else<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Wscript.Echo &#8220;Folder does not exist.&#8221;<\/p>\n<p style=\"padding-left: 30px\">End If<\/p>\n<h2>Translating VBScript into PowerShell<\/h2>\n<p>So, I could not find a direct version of the previous VBScript code in the Script Center Repository&mdash;that is probably a good thing. So you know what I did? I took about two minutes, and I pasted the VBScript script into the Windows PowerShell ISE. I then changed just a few things, and voila! I have a VBScript Windows PowerShell script. Not only that, but I went from six lines of code to four! Groovy. Here it is:\n<strong>Verify that a folder exists <\/strong>\nWindows PowerShell<\/p>\n<p style=\"padding-left: 30px\">$objFSO = New-Object -ComObject &#8220;Scripting.FileSystemObject&#8221;<\/p>\n<p style=\"padding-left: 30px\">If ($objFSO.FolderExists(&#8220;C:FSO&#8221;))<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; {$objFolder = $objFSO.GetFolder(&#8220;C:FSO&#8221;)}<\/p>\n<p style=\"padding-left: 30px\">Else {Write-Host &#8220;Folder does not exist.&#8221;}\nThis is not the most egregious thing I have ever seen. Look at some of the stuff in the earlier examples of the series. But it is still is a shame because Windows PowerShell has a built-in cmdlet that tests paths.<\/p>\n<p style=\"padding-left: 30px\"><strong>Note<\/strong> &nbsp;&nbsp;As a Windows PowerShell best practice, if a Windows PowerShell cmdlet exists to do something, use that cmdlet. Do not revert to WMI, COM, or even .NET classes directly unless there is a very specific and compelling reason to not use the Windows PowerShell cmdlet.<\/p>\n<h2>Use the Test-Path cmdlet<\/h2>\n<p>Ever since Windows PowerShell 1.0, there has been a <strong>Test-Path<\/strong> cmdlet. This cmdlet is awesome in its simplicity and its elegance. The following script duplicates the previous code in that it tests the path to a folder. If the folder exists, it returns the <strong>Folder<\/strong> object, and if it does not exist, it displays a string that the folder does not exist. Here it is:<\/p>\n<p style=\"padding-left: 30px\">Use Test-Path: Windows PowerShell<\/p>\n<p style=\"padding-left: 30px\">if(Test-Path c:fso)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; {get-item c:fso}<\/p>\n<p style=\"padding-left: 30px\">else {&#8220;Folder does not exist&#8221;}\nThere are a couple of differences. First there is no COM object to dispose of. Secondly, the VBScript code did not return an object. It created a folder object, and stored it in a variable, but it did not return the object. Here, I return the object, so there is feedback of the &ldquo;connection,&rdquo; and it permits further use of the folder.<\/p>\n<h2>Create a folder<\/h2>\n<p>In VBScript, it was really easy to create a folder. In fact, it only required two lines of code: Create an instance of the FileSystemObject, and then call the CreateFolder method. This technique is shown here:\nCreate a Folder: VBScript<\/p>\n<p style=\"padding-left: 30px\">Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<\/p>\n<p style=\"padding-left: 30px\">Set objFolder = objFSO.CreateFolder(&#8220;c:fso&#8221;)\nHey, guess what? I can do exactly the same thing with Windows PowerShell. I can create an instance of the <strong>FileSystemObject <\/strong>and then call the <strong>CreateFolder<\/strong> method. This technique is shown here:\nCreate a Folder: Windows PowerShell<\/p>\n<p style=\"padding-left: 30px\">$objFSO = New-Object -ComObject &#8220;Scripting.FileSystemObject&#8221;<\/p>\n<p style=\"padding-left: 30px\">$objFolder = $objFSO.CreateFolder(&#8220;c:fso&#8221;)\nBut you know what? As simple as this code is, I can beat it. Here is all there is to create a folder in Windows PowerShell:<\/p>\n<p style=\"padding-left: 30px\">md c:fso\nYep, that is right. Two letters, <strong>md<\/strong>, is all that is required to create a new folder in Windows PowerShell.<\/p>\n<p style=\"padding-left: 30px\"><strong>Note<\/strong> &nbsp;&nbsp;This is actually one of the first &ldquo;problems&rdquo; that I had with VBScript. In DOS, there was the MD command to create a folder. And although the VBScript is only two lines of code, it just did not seem like much of an upgrade to go from a two letter command to two lines of rather obscure code. <strong>MD<\/strong> is a function that is built-in to Windows PowerShell, and it brings back the ease-of-use of the old DOS command.\nKM, that is all there is to migrating a VBScript folder code to Windows PowerShell. Join me tomorrow when I will have a guest blog post written by Jonathan Tyler about working with event logs. It is good stuff that you will not want to miss.<span style=\"font-size: 12px\">&nbsp;<\/span>\nI 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=\"http:\/\/blogs.technet.commailto: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.\n<strong>Ed Wilson, Microsoft Scripting Guy<\/strong><span style=\"font-size: 12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Simplify your code and do not translate an old file and folder VBScript script into Windows PowerShell. &nbsp;Hey, Scripting Guy! I know there are not really established patterns and practices for writing VBScript code, or even for writing Windows PowerShell script. But there is code that I like to use&mdash;over and over again. I [&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":[38,11,51,3,4,12,155,45],"class_list":["post-3379","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-folders","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-storage","tag-vbscript-migration","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Simplify your code and do not translate an old file and folder VBScript script into Windows PowerShell. &nbsp;Hey, Scripting Guy! I know there are not really established patterns and practices for writing VBScript code, or even for writing Windows PowerShell script. But there is code that I like to use&mdash;over and over again. I [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3379","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=3379"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3379\/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=3379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}