3 Awesome Things You Can Do With Windows Scripting

Ryan Dube 09-09-2011

windows scriptingWhether you work in the IT field, as a web designer, or if you are just a student or regular office worker, Windows Scripting has something to offer you. Of course Applescript How to Use Applescript's UI Scripting on Mac Read More does as well, but my focus in this case is on Windows. When I refer to Windows Scripting, I’m referring to text files filled with code that are saved as .wsf files, which Windows is able to compile and run on the fly.


Almost a year ago now, I wrote an introduction to Windows Scripting Better Than Batch: A Windows Scripting Host Tutorial Read More which showed how much more powerful a .wsf script can be than the older style batch jobs that IT professionals have been scripting and running for years. WSF gives you the power of a structured language like Visual Basic. By default, you can create a VBScript or JScript WSF file on Windows and it will run just fine.

Beyond that introductory article, today I wanted to offer three typical tools that people often use in both a professional IT environment as well as at home. Those three tools include reading input from a text file, pinging various devices on your network, and sending email via script.

The Power Of Windows Scripting

What learning each of these smaller components will do is allow you to combine them into a larger, automated script. I’m going to step through small script samples that you can put together into a very cool automated script. What that script will do is take an input IP list from a text file, ping each of those devices, and then send an alert email if any of those devices are down.

Reading Input Files

The first step in this process is learning how to read and process information from an input text file. In this case, I’ve created a text file called IPlist.ini that resides in the same directory as the script. This file contains a list of all of the IP addresses I want to check. You can read in each line of a text file using the following script.

<script language="VBScript">
Option Explicit
On Error Resume Next
Dim strHost
Dim strCommand
Dim ReturnCode
Dim strLine
Dim oFSO, sFile, oFile, sText
Set Shell = wscript.createObject("")
Set oFSO = CreateObject("Scripting.FileSystemObject")
sFile = "c:\users\owner\scripts\IPlist.ini"
If oFSO.FileExists(sFile) Then
Set oFile = oFSO.OpenTextFile(sFile, 1)
Do While Not oFile.AtEndOfStream
sText = oFile.ReadLine
If Trim(sText) <> - Then
strCommand = sText
wscript.echo "IP Address is: " & sText
End If
WScript.Echo "The file was not there."
End If

What this code does is use the Windows file system object to open a file, and then reads one line of text at a time until it reaches the end of the file.


Pinging A Host

Now that you know how to read each IP address out of the text file, how do you go about performing a Ping withing Windows Scripting?

Pinging is a bit more complicated than reading in a text file, because you have to make use of Windows Management Instrumentation scripting (WMI). Here’s what it looks like.

<script language="VBScript">
Option Explicit
On Error Resume Next
Dim colPingResults, objPingResult, strQuery
Dim strIPtext
strIPtext = ""
' WMI query
strQuery = "SELECT * FROM Win32_PingStatus WHERE Address = '" & strIPtext & "'"
Set colPingResults = GetObject("winmgmts://./root/cimv2").ExecQuery( strQuery )
' Translate query results
For Each objPingResult In colPingResults
If Not IsObject( objPingResult ) Then
Ping = False
wscript.echo strIPtext & " is not pingable"
ElseIf objPingResult.StatusCode = 0 Then
Ping = True
wscript.echo strIPtext & " is pingable"
Ping = False
wscript.echo strIPtext & " is not pingable"
End If
Set colPingResults = Nothing

See how easy that was? When I run it, it provides a pop-up for whether the IP was pingable or not.

windows scripting


In this script, I’ve only pinged a single IP address, but all you have to do is embed that ping into the previous script after each IP address is read in from the text file, and you can ping each IP address in your list.

Sending An Email

Finally, while it’s nice to have a script that you can run that will check IP addresses and pop-up a window if there are any errors, wouldn’t it be nice to run the script daily, or multiple times a day and have it automatically email with problems?

To do that, you need to know how to send an email within script. If you research online, you’ll discover dozens (or more) ways people accomplish this. By far, the most popular is using the CDO approach.

<script language="VBScript">
Option Explicit
On Error Resume Next
Const fromEmail = ""
Const password = "xxxxxxxx"
Dim emailObj, emailConfig
Set emailObj = CreateObject("CDO.Message")
emailObj.From =
emailObj.To = ""
emailObj.Subject = "Test Email"
emailObj.TextBody = "It Works!!"
Set emailConfig = emailObj.Configuration
emailConfig.Fields("") = ""
emailConfig.Fields("") = 465
emailConfig.Fields("") = 2
emailConfig.Fields("") = 1
emailConfig.Fields("") = true
emailConfig.Fields("") = fromEmail
emailConfig.Fields("") = password
Set emailobj = nothing
Set emailConfig = nothing

The above script will let you send any text for the message body as an email to any address using your Gmail credentials. You could modify the parameters to use any other SMTP mail server you like.


Now all you have to do is put those three pieces of code together. When you do so, the script will read in each list of IP addresses, ping each one, and then send that string as the message body to a notification email. Here’s what that email will look like.

windows scripting

As you can imagine, this becomes pretty useful in the IT world, where you have an endless list of devices and servers to keep an eye on, and only 24 hours in the day. Any time you can have automated scripts that can check things for you, do it.

Give these Windows Scripts a try and see if it helps you optimize your work and make things more efficient. Do you know of any other cool things you can do with Windows Scripting? Share your thoughts in the comments section below.


Image credit: Mario Alberto Magallanes Trejo

Affiliate Disclosure: By buying the products we recommend, you help keep the site alive. Read more.

Whatsapp Pinterest

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. Jeff Carno
    April 21, 2019 at 9:51 pm

    The following line from "Reading Input Files "is highlighted in red by the VBE.

    "If Trim(sText) - Then"

    the syntax must be wrong??

  2. Anonymous
    September 16, 2015 at 12:41 pm

    What change has to be made if the email has to be send with an attachment, please comment

  3. Anonymous
    September 10, 2011 at 3:48 am

    dont know about writing down scripts but the Sikuli thing is so awesome just read about it and will try it soon, thanks mate :D

    • Ryan Dube
      September 10, 2011 at 12:38 pm

      Awesome - thanks for your comment.

  4. Anonymous
    September 10, 2011 at 3:03 am

    did you write on purpose?? cuz in the screenshot it reveals the id *evil laugh*

    • Ryan Dube
      September 10, 2011 at 12:38 pm

      I do that occasionally - usually my biggest concern is scraping so I make sure text will have the address x'd out, but it's hard to catch every spot in the screenshots that display it. :-)

  5. guest
    September 9, 2011 at 10:57 pm

    What versions of windows can you run windows scripting on?

    • Ryan Dube
      September 10, 2011 at 12:37 pm

      Good question - from my own experience I know it works on Windows 7 and Windows XP. Beyond that I really can't say - but I think that covers a fairly wide range of what people would need?