JoiWiki » Developer » Using Windows » Windows Scripting Windows Scripting

Windows Scripting

Windows scripting

Script files are great, they enable you to run bespoke code on a machine to achieve all kinds of things that would be difficult or time consuming to do otherwise without needing a full coding environment to create and without needing to compile anything. The fact that nothing needs to be compiled means that the operative code is readily available to inspect and edit at a moment’s notice so a few well placed scripts can help an awful lot.

some of my favourite aspects of writing script files are:

  • They’re quick to create and run, not needing to load a large application or many libraries
  • They don’t have forms associated with them by default and lend themselves to simple one-off tasks
  • As small chunks of code they avoid the tendency to create sprawling data structures
  • They give access to the power of code, meaning the ability to repeat actions, look for things automatically, create and delete files/folders on mass, fill those files, open one of them, communicate with databases, pass credentials – what did you want to do?
  • They don’t need heavy IDE software on the machine you’re using or import projects that have been created on other machines, even notepad does the job in a pinch
  • A well crafted script can be adapted to suit your needs on the fly

But with great power…

There’s a horrible downside to all of these wonderful things, in fact there are many and in the wrong hands a little knowledge can wreak havoc on a computer or a network in ways that simply wouldn’t be possible using menus and filling in forms normally so extra care is needed. Before you embark on an afternoon figuring out how much you can automate and do (… just me?) it’s good to be aware of the following annoyances/issues and outright risks that you’ll come across:

  • With no IDE you get no intellisense and unless you’re using something like the excellent Notepad++ you won’t even get syntax highlighting
  • All the errors! it can take a while getting used to writing code when errors are only discovered at runtime and can be incredibly frustrating
  • If you’re used to coding in a fully fledged object oriented environment it can be a little teasing in that it can be difficult to achieve more complicated behaviours – that said there’s a tool for everything and if this is happening maybe you should crack open your favourite IDE after all
  • Anybody can take a look to see what a script is doing if they open it in a text editor, don’t put in admin account credentials or any other sensitive information if the script will be accessible to other people
  • The above also means that anyone can edit your scripts if they want to make it do something else (read: not what you wanted)
  • one of the things that some of my scripts do is remove any readonly permissions on a file before deleting it so that it can be replaced – you can imagine the damage that can be done to an entire machine or network directory with only a few lines of slightly misconceived code – there’s no error messages and no warnings.. be careful!
  • A folder full of scripts with different revisions and options can be horrible to manage

So scripting in windows

With all of the above taken into account there are three scripting filetypes that can be used in windows out of the box; batch, VBS (vbscript)and the later PowerShell and for a lot of tasks any of these three can be used but they do different things and the code is run by different run times which can make a difference in the code you write and what can be achieved.


Batch files (wiki) have been around since the dawn of windows and are ways to put together commands for the command line – this means that anything that you can achieve through the command prompt can be achieved with batch files and that’s a lot! although of course the command line takes a series of individual commands which can make complex operations difficult so batch files can’t be relied on for everything. For more information here’s the , a quick example of copying a single file in batch would be

Copy "C:\Users\MyName\Documents\MyWork.docx" "D:\Work\Subjects\Research\MyWork.docx"



VBScript (wiki) is modeled on visual basic which means an object-oriented environment, it’s much easier to create variables, run through loops and the like which is a great advantage over batch files and have been used for many years but it can be very verbose and require a lot of development to get things done, but it can be nice to lay out scripts in a maintainable way. Here’s a simple copy file script

Const SourceFile = "C:\Users\MyName\Documents\MyWork.docx"
Const DestinationFile = "D:\Work\Subjects\Research\MyWork.docx"
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile SourceFile, DestinationFile, True
Set fso = Nothing



To combat this since windows xp, Microsoft have baked compatibility for PowerShell (wiki) into windows as an enhancement, I’ve not used PowerShell much and the earning curve is said to be steep but it can be very powerful and very concise, for my part I first started coding in a VB.Net environment so although it could be described as antiquated I often find VBScript the quickest to put together quick scripts with. To compare to the above in this example the code is much cleaner

Copy-Item -Path ‘C:\MyFile.txt’ -Destination ‘C:\SomeOtherFolder’


Actually using them then

Actually creating and running the scripts is easy;

  • Create a new text file
  • rename it and set the extension to be either .bat, .vbs or .ps1

Done! I’ll post some of my scripts for you to use but I’ve found it nice to automate things like mapping network drives that keep falling off, copy files from one place to another, create folder structures without having to manually open and right-click within each one and as ever there’s a wealth of information around.


Table of contents:  
Created by admin. Last Modification: Sunday December 23, 2018 12:22:05 GMT by JBaker.