I am a huge fan of scripting. If I have to do a certain task on a computer often, then I would definitely try and create an automated script so all I have to do is run one file.
Additionally, having these scripts can give me an edge over my competitors. Lets say I have 2 computers to repair on my workbench. One of them has a tricky issue that requires most of my attention and the other one requires me to do something I have done a hundred times before.

Since I have made a script to do the task I have done a hundred times before, I can focus my attention on the other computer. If the script does the task quicker than I can do manually, it gives me an advantage over my competitors since I can either lower my price because it took less time; or have a fixed price and use the time I saved to work on another computer resulting in more computers fixed on the day.

In this article I am going to show you some scripting basics so you can make some useful computer technician tools for your business. Alright, lets get started. To begin, we’ll work with good old Windows BATCH (.BAT) scripts.


Disclaimer: This article is for people with a good understanding of Windows only. I highly, HIGHLY recommend that you do scripting in either a Virtual Machine environment or a dedicated, unimportant machine in case you make a mistake with one of your commands and do some damage to the system. Technibble or I cannot be held responsible if you do damage to the operating system or lose any data.

Now we have that out of the way, a task that computer technicians frequently have to do is backup their clients data, so lets make a little script to do that.
I am going to want the clients CDKeys for their operating system and maybe Microsoft Office if they have it. While there isnt a command to do this in Windows batch scripting, I can use Nirsofts ProduKey to get it. I really like Nirsofts software because just about everything they release can be run from the command line allowing us to work them into scripts.

Download ProduKey from here (32bit) (64bit version here) and extract the zip somewhere you will remember like your desktop.

In the same folder as the extracted ProduKey.exe, create a new notepad text file and open it. Now we need to find out what the commands are for ProduKey so we can make it work from a script. Goto the Produkey website by clicking here and if we scroll half way down the page we will see “Command-Line Options”. The first table has commands for getting information remotely but we are going to assume we are actually at the computer when we do this. So, look at the first line of the second table and we will see:

/stext <filename> - Save the list of product keys into a regular text file

In our text file we made just a moment ago, write:

produkey.exe /stext Keys.txt

What we are telling the script to do is to run ProduKey and write the CDKeys to a text file called Keys.txt

Now, save this text file as a .BAT file (or save as a TXT, then change the extension to .BAT) in the same directory as the ProduKey.exe

scripting-2-pt1

If you did it correctly, when we run this .BAT file it should show a black command window very quickly and a file called Keys.txt should appear. In this Keys.txt file you should see the CD Keys for the installed Microsoft software.

This is a very simple Batch script but lets take it even further. Lets grab the Mail settings too using Nirsofts MailPassView.

Download MailPassView here and extract it to the same directory we put ProduKey.exe in. Now lets goto the MailPassView page here and find out what the command line options are.

It appears that we can also use the /stext <filename> command on MailPassView as well. So lets edit our .BAT file again in Notepad (Right click and choose Edit if you closed Notepad) and add our MailPassView command on a new line.

produkey.exe /stext Keys.txt
mailpv.exe /stext Mail.txt

When you save and run the BAT file, two files called Keys.txt and Mail.txt should be created containing the information we want. There are lots of programs that allow you to do things from the command line like this, but I’ll get to them in a later part of this series.

You know, since I am working with many computers a day, I am going to forget which computer these Keys.txt and Mail.txt belong to, so I am going to separate them.

Microsoft Windows has many built in variables you can use for scripting purposes. If you are new to programming, variables are values that change.
Here is a good example of why we want to use variables. Lets say I wanted to access my desktop in a script. I could do the following:
cd “c:\Documents and Settings\Bryce\Desktop”

But there is one big problem, the computers I will be running this on wont have “Bryce” as a username, it’ll be something else like Admin, Owner, Jeff, John, Jill etc..

So, I’ll need to use a Windows variable called %USERPROFILE%.
Windows will replace this variable with “c:\Documents and Settings\Username” and the Username part will be replaced with the current users Username. If the current user was John, %USERPROFILE% would become “c:\Documents and Settings\John”. So if I wanted to access the current users desktop, regardless of what the username is, I would use: cd “%USERPROFILE%\Desktop”

For future reference, here is a good source listing the Windows Environment Variables.

Now lets get back to separating our Keys.txt and Mail.txt files for each computer. I am going to use the computers name to separate the text files. To do this I am going to fetch the computers name using the %COMPUTERNAME% variable and make a directory of that name, then place our text files in there. The command to make a directory is “md” or “mkdir”.

I personally prefer mkdir so I am tell mkdir to make a directory with the name of the computer, but I am going to put this at the top of the script. The reason why we are going to put it at the top of the script is because we need to make the directory first so next lines can place their files in it:

mkdir "%COMPUTERNAME%"

If my computer was named Bryce-PC, the directory that was just made would also be called Bryce-PC. Now I need to tell ProduKey and MailPassView to put their information in this folder so I am going to change the location it places the files to "%COMPUTERNAME%\Keys.txt". I have also added quotation marks around this variable because if the computer name has any spaces in it, it wont work. This is what the entire script should look like now:

mkdir "%COMPUTERNAME%"
produkey.exe /stext "%COMPUTERNAME%\Keys.txt"
mailpv.exe /stext "%COMPUTERNAME%\Mail.txt"

Scripting-pt1

Save these changes and delete the old Keys.txt mail Mail.txt files that might have been created earlier so we dont get confused. Now run the script.

It should create a folder with the name of the computer you are running it on and have the two text files inside it. You could place this script (along with produkey.exe and mailpv.exe of course) on your USB drive and run it on any Windows based PC.

Here is the complete set of files and scripts we talked about above if you are having trouble and need to see what it should look like. I have included the Help Files in this zip file to comply with Nirsoft’s licensing, but they are not needed. However, they do have some command line information in them if you dont want to use the Nirsoft site. Be sure to extract the zip file first before running anything in it.

Thats the end of the first part of our scripting tutorial. In the next part I’ll show you how to create a script to backup the main parts of a system (Desktop, Favorites, My Documents etc..).

You aren’t just limited to creating a backup script or even using Nirsofts tools, you can use any application with command line options but this is a good starting point. If you wish to continue experimenting with this before the next article is posted, maybe you could expand the script to fetch the saved Wireless Keys as well? (if one exists, otherwise it would be blank).