Ed Wilson: On translating VBScript to Windows PowerShell

One of the things that I often discuss with people who write VBScript code is the process of translating VBScript code to Windows PowerShell code. In fact, several years ago, I was approached about writing a utility that would automate the process. I thought about it long and hard, and in the end decided to forgo developing such a tool.

In my Windows PowerShell Step by Step book, which was published by Microsoft Press, I have an appendix that is called the VBScript to Windows PowerShell conversion guide (there is a similar document on the TechNet Script Center). The conversion guide is useful from a getting started perspective, but should not be taken as best practice, or even as authoritative.

I even wrote a series of Hey Scripting Guy! articles that talk about converting VBScript to Windows PowerShell (one such example is here).  But in all actuality, I have translated less than 1% of the more than 10,000 VBScripts I have written in my lifetime.

There are several reasons for not translating VBScript to Windows PowerShell:

  1. Waste of time. If the VBScript works, leave it alone.
  2. Better way of doing things. Many VBScripts wrestled with things such as opening files, retrieving property values of objects and writing output to the console. These types of tasks are nearly automatic in Windows PowerShell.
  3. There are too many new things to script using Windows PowerShell. Many things that were impossible to do using VBScript are now possible using Windows PowerShell. There is a seemingly endless list of new things to script. I simply do not have time to go back and translate VBScript code.

Reasons to translate VBScript to Windows PowerShell:

  1. You are just learning how to script, and would like to have an example to follow along with.
  2. You have a rather complicated COM based script, such as Word or Excel automation, that will not be significantly different when written in Windows PowerShell.
  3. You are in the process of phasing out VBScript from your network, and you need to quickly migrate your existing scripts.

Probably the biggest reason not to translate from VBScript to Windows PowerShell is because you will want to take advantage of the new features that Windows PowerShell offers. Often the difference between a 50 line VBScript and a Windows PowerShell script can be as many as 49 lines of code. You can save yourself an awful lot of work once you learn how to allow Windows PowerShell to do the work for you.