What scripting languages are useful in Windows?

48

10

What scripting languages are useful for Windows automation?

abc123

Posted 2011-05-25T04:13:15.050

Reputation:

Question was closed 2011-05-26T17:11:59.423

10I gave +1 because I can see an interesting question lurking behind what you wrote. As it is though, I think "main" is an unfortunate word choice that you should edit to make less vague. Perhaps "most commonly used", "best-supported", or "recommended", depending on what you mean. Welcome to SuperUser! :) – Brennan Vincent – 2011-05-25T06:25:52.983

2@Brennan Vincent: That's a great suggestion, but changing the word "main" at this point may detract from the spirit of the answers. I think your comment speaks volumes on this matter, and that the question is better left as is. (+1 for you for that, by the way.) – Randolf Richardson – 2011-05-25T07:35:24.103

1At the moment I think this question falls into the subjective category. However with a little modification of the wording as Brennan suggests I think it could be quite an interesting community wiki entry. – Joe Taylor – 2011-05-25T11:06:13.967

1Looks subjective to me, also. For scripting languages, "main" or "best" or "preferred" would differ based on the particular task being accomplished, and an individual's skill and experience. – Iszi – 2011-05-25T12:58:05.423

3I take the question to mean: *what is the native scripting language for Windows today?, ie, what scripting language has built-in hooks and functions into the OS as opposed to requiring APIs, ActiveX, etc. to get things done?* – Synetech – 2011-05-25T13:02:29.287

To prevent this from being closed I'm removing subjectivity from question & answers... – Tamara Wijsman – 2011-05-25T20:18:15.797

Answers

36

  1. PowerShell
  2. Windows Scripting Host
    a. VBScript
    b. JScript
  3. Batch Files

PowerShell (v2.0)

As of Windows 7 and Server 2008 it's reasonable to consider PowerShell as it ship with Windows, and it is also fundamental to administer server software such as Lync and Exchange as there is a whole set of commands availble for them. 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 other are available as open source, 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 take advantage of the WMI service and the objects it exposes.

Example: This creates 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 If

JScript

You can also write the same script 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.

Nick Josevski

Posted 2011-05-25T04:13:15.050

Reputation: 6 747

7As for Windows 7 and Server 2008, it would be Powershell 2.0 – sshow – 2011-05-25T09:45:55.803

@sshow agree about being specific about 2.0 – Nick Josevski – 2011-05-30T23:35:38.253

"They need no intro." But the do need an extro... – glenn jackman – 2014-03-27T17:16:13.157

16

I 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.

Randolf Richardson

Posted 2011-05-25T04:13:15.050

Reputation: 14 002

At some companies e.g. the bank I currently work for, the Execution-policy prevents PS scripts from getting executed. Thus I'm learning batch to translate my PS scripts. So there are some situations where Batch is still needed... – T_D – 2016-02-11T15:29:37.877

I agreed with your answer in 2011, but with Windows 10 Creators Update, Microsoft made PowerShell the default for when pressing Win+X, typing 'cmd' in the Start Menu, and holding Ctrl when right-clicking in Explorer.

So I now consider Powershell the default scripting language for Windows, since Windows 10 Creators Update, and BAT/CMD legacy scripts. – Jonas – 2017-09-15T07:37:46.363

10If by "main" scripting language you mean "oldest and most widely used", this is definitely the right answer. However, cmd really is a monstrosity, easily the WORST shell scripting language that I've ever had the misfortune to use. – JSBձոգչ – 2011-05-25T12:59:14.760

3I too use batch files when the script needs to be used on other versions of Windows. If you are only dealing with Win7/Server 2008 then PowerShell is the way to go. – jtreser – 2011-05-25T13:05:33.470

5+1 I know MS wants powershell to be it, but for now if you want to send someone a script that is running "windows", you have to send them a DOS batch file. PS is great for certain versions of windows for certain tasks and they expose a lot of power, but batch files are universal. – Alan Jackson – 2011-05-25T14:06:24.657

10

That 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.

MaQleod

Posted 2011-05-25T04:13:15.050

Reputation: 12 560

2+1 for mentioning cross-platform scripting languages (Perl is my favourite, and I know that Python has been gaining popularity over the years too). – Randolf Richardson – 2011-05-25T18:04:31.563

2+1 for Perl. It is useful on many platforms and it has some Windows-specific modules. – RedGrittyBrick – 2011-05-25T22:59:09.203

1

I would add that TinyPerl is a very useful way to embed simple Perl script with not too many dependencies. It will compile to a tiny EXE file on Windows. http://tinyperl.sourceforge.net/

– Benoit – 2011-06-04T15:09:51.687

4

I 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.

monoceres

Posted 2011-05-25T04:13:15.050

Reputation: 225

1AutoIt is pretty cool, especially for UI functions (acting like a user typing into a dialog box, pushing buttons, etc.) but it would be a real stretch to call in the "main" Windoze scripting language. These days most of my colleagues (in a team of 20+ Windows Admins) haven't even heard of it. – ewall – 2011-05-25T16:49:54.173

Like monoceres, I use AutoIt. It's extremely versatile for even rapid prototyping and testing concepts in a few minutes. Amongst that, it's designed for automation, which is what I see a lot of people using Powershell for. Give it a try :)

– James – 2011-05-25T16:17:21.490

4

I 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.)

Synetech

Posted 2011-05-25T04:13:15.050

Reputation: 63 242

[shuddering intensifies], I used to write that. You know you can run arbitrary WSH scripts from IE? Isn't that quaint. Nice how they open up so much of their api to the web. (Disclaimer, this cynicism is dated by XP-SP3) – ThorSummoner – 2015-05-14T17:02:38.007

The story about WSH and IE is not really that simple: https://msdn.microsoft.com/en-us/library/bb250473(v=vs.85).aspx (just one example of restrictions, depending on security zones) And IE 11 doesn't even support VBScript anymore, other than in legacy mode. Don't rely on IE to execute WSH scripts.

– Jonas – 2017-09-15T07:45:05.853

2

The 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.

Uri

Posted 2011-05-25T04:13:15.050

Reputation: 318

1Python is a very good alternative to perl, though the things you'd use the two for are pretty much identical – Phoshi – 2011-05-25T22:32:38.603

1

In 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.

EKS

Posted 2011-05-25T04:13:15.050

Reputation: 258

You seem to be expressing a keen desire for PowerShell to take over. Would it be fair to assume that you "want it to be" PowerShell? ;-D – Randolf Richardson – 2011-05-25T18:01:38.777

I prefere powershell over vbscript yes. – EKS – 2011-05-25T18:18:26.430

1

I 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

Jamie Cox

Posted 2011-05-25T04:13:15.050

Reputation: 639

1

It 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.

Konstantin

Posted 2011-05-25T04:13:15.050

Reputation: 183