top of page

Useful Windows Scripting Tools for Lab Automation

Updated: Aug 5, 2020

Batch scripting provides the classic command prompt (cmd) control easy enough for anyone to get started with. This is a great way to learn about old school operating systems, and how those tools can still be more useful that the Windows user interface when you know what you are looking for. While you can manually enter the commands as shown on this post into the cmd prompt, you can also write them in a text editor to be recalled more times. The text file that folds the batch instructions is the script. Scripts can be executed by clicking on them, like any other program. You can also use cmd prompt and batch scripting to fire off other scripts.

To actually create the script is simple. All you need to do is use a text editor like Notepad to record the text. I like to test my code out on the cmd prompt, and then I know my script will generally behave as intended. Be sure to save your file name as "YOUR_SCRIPT_NAME.bat", for the '.bat' file extension tells the windows operating system that this is a batch script, and so when clicked it should be executed. What's great about scripting in batch is the error handling built into the process.

Here is a totally non-exhaustive list of scripting commands that I've found very useful in the lab setting. Most of these scripting techniques I'm referencing are 25+ years old. The great thing about that is the vast array of resources out on the web. If you want to explore anything further or have any questions, I recommend searching around google for batch script techniques, and chances are you will find code similar to whatever you are doing that you can massage into place in your script.

Handling Files and Directories

Setting up new directories and deleting on the fly is another invaluable and simple scripting tool built into cmd. For making a directory, you simply use the command 'mkdir NAME', where the NAME parameter you would specify the name of the directory you are creating. An important thing to note, cmd prompt keeps track of your current location in your system. You can use the command 'cd' to change that directory location, as shown below.

When you use mkdir create a directory, if you do not specify the full path of that directory, it will default to create the directory in your current cmd location. To get around this, I opt to specify the full path every time to avoid any ambiguity. You can see the automatic error handling that steps in if you try to create an already existing directory.

Like with directories, cmd prompt is also very useful for any sort of quick file handling via commands such as copy and delete. This can be very useful as a script because the tool RoboCopy allows you to do this with a variety of options. I'll show an example of a simple script below utilizing this. An example of a useful tools leveraging these simple commands could be written to readily configure a new PC with all the working directories needed, and you can copy over entire folders with file contents as well.

Managing Services

Often your instruments are controlled by software running on PCs, through software systems provided by the vendor. These normally are made in a way that use software service running in the background. For these, some vendors don't offer a software solution for controlling those, you have to do so in the task manager. You can also use cmd prompt to start, stop, query, and even create/delete services (be real careful with that last one) by calling the function and then providing the service name string. This is done in cmd with "sc" (service) followed by the action command. For example:

sc stop CompoundPlatingClientService - stop the service

sc start CompoundPlatingClientService - start the service

sc query CompoundPlatingClientService - display the current status of the service (stopped, running, etc)

Here is an example of a typical response that you might see if you try to interact directly with services. This is because Windows has built in protection against scripts that do not provide the appropriate credentials for the permissions required by the requested actions. I'll show you a scripting trick to get around this further on in the post.


Two of the most useful networking commands that I've found are 'map' and 'ping'. Ping is quite simple, and it allows you to discover devices on your network if they are configured properly, that can be an essential tool used to configure devices on your lab's network. There is so much more you can do with this, and commands exist that allow you to take action on other computers on your network. I haven't found the need for this much yet, and prefer to use a remote desktop software like TeamViewer to pop into my PCs and fire off scripts from there (like file backup and archiving scripts). However experts out there are making that strategy look silly, by flexibly handling stuff like that on all their PC's from a single interface.

Map is a very common command if you are working with instrument control PCs that are not directly managed by the IT network. In our lab, this is ideal that we manage these computers ourselves because our IT restrictions for access are too limiting for our needs. So how do we still gain access to the company directories that we need? By mapping those directories as a drive on your PC. You need to supply some extra parameters for this one, because it's likely that you will fail unless you provide the appropriate access credentials (username and password) that your IT needs to provide for you.


Organizing your code is important to make things easier to read and debug. I like to label sections by including comments, done by including '::' at the start of the lines with the section header. This lets me know what each section of the script is doing, and the script then also knows that this is a comment and not a line to be executed. An example of this is shown in the script below.

Pause is a useful command that freezes the command prompt results when it arrives to this step. This is very useful for debugging, because it will give you enough time to read the results before the script proceeds on with it's error handling. I generally use 'pause' to develop and test scripts, but seldom does a pause exist in a validated script. When a script is paused, it will continue when you tell it to. The script below on the right shows the pause implemented at the bottom, and the cmd prompt on the left shows that in action.

Timeout is a temporary pause, and you need to include a parameter here to tell the script the time in seconds that you want the timeout to last. This is also shown below, and the cmd prompt on the right will actually count down your time period (these shown here say 'Waiting for 0 seconds' because they have already successfully counted down and moved on. Like pause, the timeout will allow you to proceed from the prompt by pushing any button.

Echo is another useful tool shown above, and this relates to the information that is displayed on the cmd prompt when the script is executed. The default is to display everything, both the command entered and the result. You can see in the script above the first line of code is 'echo off' which literally tells the script to turn off that default display of every step. Then you need to call echo again in each line that you want the cmd prompt to display. This is shown where in the script above, where I've turned off the default echo, and only display certain messages that I craft to describe the process. Notice that the cmd prompt still shows the command results (both failed in this case) while echo is turned off.

Integration of Scripts

Stitching tools together as needed is where the magic happens. It's quite simple to call a script from another script, and in doing this you can chain together simple scripts into more complex processes. You can use the concept of parameters to feed values into the script and have it react accordingly. Here is an example of a script that utilizes a parameter to pass a required security key into the script to create an associated software service. The key is listed as a parameter to the 'sc create' command, but that is fed in directly as the parameter (denoted in the script as %2, where the % sign tells the script to expect a parameter. This is apposed to beingn being a hard-coded parameter like the file path and website listed in the same script. This means the script could be fed any valid name, and the resulting service would be created with that name.

Visual Basic Scripting (VBS),

I like to use this to further automate tasks on the PC. It's pretty oldschool, but still useful! Just a few gems that I've found particularly useful:

Keystroke command lets you interact with the keyboard for input of text, even control buttons like Enter. You can also control the mouse through VBS, which makes automating repetitive and consistent workflows automatable. Of course this command requires the input of a parameter with the actual keys to engage, and this can include action keys like 'Enter' and 'Backspace'.

Pause - give the computer time to react to time out your script appropriately. Like the timeout function in batch scripts, this requires a parameter in time, but this is entered in milliseconds. Getting the timing right is important if you need to await PC response to previous commands, like booting up another script or program.

You can readily launch batch scripts or other programs through VBS, as shown in the script above where I'm actually doing little more than calling a batch script to execute. I've also been known to leverage the windows task scheduler to automate the launching of my VBS scripts on the basis defined when I create the task. Pro tip there is to use VBS script like the one shown above, because this VBS script is coded to launch that batch script in silent mode, without the cmd prompt ever popping up. This is ideal for automated processes running in the background all day, because otherwise the cmd prompt will flash at you every time it's executed.

VBS has the capacity to get somewhat complex, here is an example of a script written to display a dialog box, then acquire the name of the directory selected to pass that file path along into other applications downstream. You can see that even for a simple task like that, the VBS script is reasonably complicated. In fact, for something like the purpose of grabbing and passing along file names, I'd certainly opt to use a tool like Windows Forms. I'll show you just a bit of the power and flexibility there down at the bottom of this post.


Here you have a next level scripting language that builds off of the old batch and VBS scripting styles, but includes so much more functionality. I'm not even going to try to scratch the surface here, because honestly I'm no expert and haven't yet needed to fall back on the extra functionality that Powershell provides over the traditional command prompt.

Just as an example though, I like to add a single line of PS script to a lot my batch scripts to ensure that I have admin permission upon execution. In doing this, you see that the script is actually getting launched through Powershell, and all of the batch script commands are still handled appropriately. Here is the script shown above, and you can see that since we are using parameters, we have to include those in the permissions command to allow Powershell to read and interpret those.

Here the parameter being passed in to provide a 'Vantage Key' is referred to throughout the script as '%2', but in the first command argument to engage powershell, there is no '%2' listed. This is because there is only 1 paramter, but we are also executing the script with a new parameter to ensure our admin credentials.. So %1 is actually the second parameter, and needs to be referenced as %2 in the following code. Programmer's love to start counting on 0 instead of 1.. Doesn't make any sense to me, but I try not to overthink it.

Windows Forms

Using C# is another way that you can build tools, but a bit more complex. I recommend Windows Forms with Visual Studio as the programming software because it provides a somewhat intuitive user interface development platform for making custom application interfaces, and then you write C# code to drive the application using a very streamlined set of tools. Just as an example of some possibilities here, shown below is an extremely simple application that I made to monitor and interact with services tied to our lab automation processes for this particular instrument.

Actually in this case for easy of programming and built-in error handling, these buttons for controlling the services all tend to actually just launch existing batch scripts to handle that work for us.

This is a clever way of getting around the built in restrictions that require more complex code to actually obtain and provide appropriate permissions for making such changes on the PC.. this is a necessary protection from viruses and malware. However if you are using a PC with admin privileges already set, just call the batch script from the C# application to do the heavy lifting. Check out the images below to see how I went about that. As it turns out, a few pictures are worth approximately 300 words.

17 views0 comments

Recent Posts

See All
bottom of page