What scripting languages are useful for Windows automation?
69 Answers
Windows has 3 built-in solutions to consider:
- PowerShell
- Windows Scripting Host
a. VBScript
b. JScript - Batch Files
PowerShell (v2.0)
As of Windows 7 and Server 2008, it's reasonable to consider PowerShell as it ships with Windows and is also fundamental for administering server software such as Lync and Exchange. There is a growing list of features available; see Script Center for details.
Windows Scripting Host
Windows Script Host (WSH) is technically a scripting host for ActiveX Scripting Engines. VBScript and JScript (Microsoft's implementation of JavaScript) are the two engines installed by default. Many others are available from the open source community, including Perl, PHP, and Ruby.
VBScript
Using similar syntax to VB6 and VB.NET, VBScripts lack easy access to .NET classes (Powershell is written on .NET so it has access to all the .NET functions). VBScript mostly takes advantage of the WMI service and the objects it exposes.
Example: creating a restore point.
Set wmi, whatName, errResults
wmi = GetObject("winmgmts:\\.\root\default:Systemrestore")
whatName = InputBox("Enter a name for the Restore Point", WScript.ScriptName)
errResults = wmi.CreateRestorePoint (whatName, 12, 100)
If errResults <> 0 then Wscript.Echo "Error " & errResults & " : Unable to create Restore Point"
End IfJScript
The same script can be written using ECMAScript:
var wmi = WScript.GetObject("winmgmts:\\.\root\default:Systemrestore");
var whatName = WSHInputBox("Enter a name for the Restore Point", Script.ScriptName);
var errResults = wmi.CreateRestorePoint(whatName, 12, 100);
if(errResults != 0) { WScript.Echo("Error " + err + " : Unable to create Restore Point");
}Command Batch Files
There is of course the good ol' Batch File. They need no intro.
3I still write DOS batch files, even in Windows 7. For most tasks, this still works quite well, and it's very simple to work with (also, many of the DOS commands such as "FOR" have been improved over time and provide more options and functionality that weren't available more than a decade ago).
For me, a DOS batch file is still the main scripting language for Windows (it certainly is traditional), but everyone has needs and preferences that differ. There are many things that DOS batch files can't do (scripting languages can pose limitations too), and for the rare occasions where I've encountered this I then look at what my other options are (often it's Perl, or sometimes it could be to write a small program or an entire application).
Understanding what you need to accomplish is a very important step in deciding which tools to use. Familiarity with the tools is another important aspect that can limit your options. If you're trying to decide which scripting [or programming] language to learn, then hopefully this will be helpful to you.
5That depends on who you ask. Some will never leave batch, some love vbscript some love powershell, others like AutoIt. Then there are the platform independent ones like Python and Perl that some will swear by for everything.
3I suppose the official scripting “language” for Windows would be (and has been since Win95) Windows Script Host.
Technically, WSH is not a language unto itself, but rather an environment (not GUI) that exposes COM interfaces to allow you use (almost) whatever scripting language you want, to do OS things, even things that have no native way built into the language (for example, Ruby has no built-in way to log into Windows or TCL/TK has no built-in way to show the Run dialog).
You may have seen some WSF files which can include multiple languages in one file, but usually WSH scripts are written in one language and distributed with an extension corresponding to the scripting version of that language (eg VBS for VisualBasic scripts, PYS for PythonScript, etc.)
2I would say AutoIt, it provides very fast and easy developedment on windows and is very tight coupled with it. This means that tasks that usually take rather much code in other languages can be done in literally one line in autoit. Features such as direct compilation to exe's is also very useful.
3The other languages pointed in this question (VbScript, PowerShell, Batch) are very popular and supported. If you already know a scripting language and feel comfortable with - by any mean go with that.
If you are about to invest in picking up a new language, I'd suggest two languages for you, depends on the job you are trying to do:
JavaScript (via WSH) - use JavaScript if you want to use various Automation Object by instantiating them, and calling into their Object Model. You can create an Automation Object for pretty much everything, from Exchange server to WMI to Office documents. There is a debug support with Visual Studio (with the /x argument). Also, an investment in JavaScript will prove to be useful in HTML.
Perl - use Perl if you can to launch other programs, manipulate their output, perform regular expression. Although it seems this language is currently in the decline, it is still a very popular, very well supported with a big community language. You should also consider this language for communicating with other services (not necessarily Windows), such as a JIRA on the enterprise, or EC2 on Amazon. Debugging is supported in eclipse with EPIC, which is also a good IDE environment (syntax coloring, etc.). There are modules in CPAN for everything. Perl knowledge will prove to be useful when having to do automation on non Windows machines.
1In the early days, there was VBScript followed by Batch, in the corporate environment with pure Windows 7 and Server 2008 R2 the new scripting language Powershell is raising a lot of attention.
2I second the recommendation for Perl, if you're up to writing some Perl scripts. People I work with have successfully used and recommend CLR Script (a bit old), and for scripting web interaction,iMacros
0It could be almost anything, depending what you try to automate. It will vary from batch files and PowerShell to autohotkey and Selenium (if you need to automate some GUI related tasks).
Usually it is not a problem to learn one, if you need to add some modifications or support already written scripts. If you need develop something new - choose language/way based on requirements, and what you already know.