Are there many tasks in Windows that you perform regularly? Get started with your own batch scripts, with which you can run the tasks automatically. They are as old as MS-DOS, but still work in Windows. Learn how to write some custom scripts to run your computer like a pro.
Tip 01: Scripts
You can use scripts for different tasks. In most cases, scripts come in handy when you have recurring tasks that you don't want to do manually anymore. Think of requesting computer information or requesting the contents of a folder. Creating a script does not have to be complicated, as you will read later in the article. Each line in a script runs a command. With the command pause you can, for example, pause a script, which is useful if you want to write several commands in succession in the same script. If you have – perhaps from the past – experience with assignments in MS-DOS, you will now benefit from this knowledge again. With a script you can execute almost all commands as you were used to doing manually. Popular commands like del, cls and Ren are perfectly feasible. Not familiar with these assignments yet? No problem: it is relatively easy to build up the necessary basic knowledge.
Tip 02: Construction
A script consists of a number of lines of text that instruct the computer to perform certain tasks. You don't need much to create a script. You don't need more than the built-in Notepad. Open the Start menu, start the word Notepad and open the app of the same name. You can quickly create a simple script that displays a sentence, for example. Type the following:
@ECHO OFF
ECHO This is my first self-written script
PAUSE
Then you save the file, it is important to use the correct extension. Choose Save file. Choose at Save as in front of All files. Give the file a name, with the extension .bat. For example: Script.bat. Time to test the script. Close the Notepad file and double click on the file Script.bat. A new window will open and the script will display the sentence. Pressing any key closes the window. To edit the script at a later time, right click on it and choose To process. Notepad will open and you can make the adjustments.
Frequently used commands
ECHO Choose ECHO OFF if you only want to show on the screen what the result of a certain command is (and hide the commands themselves). For example, do you choose an assignment such as ping tipsentrucs.nl and do you have the assignment for that ECHO OFF then the window will only show the result of the ping command.
Add an at sign (@ECHO OFF) to also get that first command ECHO OFF with the command prompt.
CLS Clear the current Command Prompt window so that you start with a clean slate. This command is useful if you run several scripts in succession and want each time to start with an empty window.
TITLE: If you want to give the Command Prompt window its own title, use this command, followed by the title. For example:
TITLE: This is my own script
PAUSE Pauses a script's execution and allows it to be resumed later.
:: You use these two colons to place a comment in the document. Handy if you create multiple scripts yourself and want to understand them at a later time. Also useful if you share the script with others and want to explain it. For example:
:: This script controls the operation of the computer
COPY This will copy a file or folder to another location. For example:
COPY Script.bat C:\Docs
EXIT This closes the Command Prompt window.
Tip 03: In case of problems
Do you have network problems, for example because the internet connection is lost? Normally you run a command like ipconfig /all to check the status of the network hardware. Or do you use a command like ping to check the reachability of a system on the network. Such commands fit perfectly in a script, because you can execute them one after the other. We can set up the script like this:
@ECHO OFF
ipconfig /all
ping tipsentrucs.nl
tracert tipsentrucs.nl
PAUSE
Anyone can write their own scripts relatively quicklyTip 04: Notes
Especially with longer scripts it is useful (and neat) to occasionally add a comment in the script. This way you know later what the function of a script is, but other users can also work with your scripts. After all, the comment explains what is happening in the script. A comment is never "executed" by the script. To post a comment, type :: (twice a colon, without a space), followed by a space and the actual comment. Each line can contain a comment. This is what that line looks like:
:: This script controls the operation of the computer
Tip 05: Text file
Sometimes a script generates important information. You can choose to display the information on the screen (as in tip 3), but you can also have the information saved in a text file so that you can review it later at your leisure. This is also useful if you want to compare information over a longer period of time. For example, if you have written a script that checks the ping speed of the internet connection and you run the same script again after some time. You use the >> characters for this, followed by a space and the name of the text file to which the information must be written. For example:
ping tipsentrucs.nl >> registration.txt
The results of the ping command to tipsentrucs.nl are written to the file when the script is run Registration.txt. You can then view this file with Notepad.
A usable script then looks like this:
@ECHO OFF
:: With this script I check if the internet connection is working properly
ipconfig /all >> registration.txt
ping tipsentrucs.nl >> registration.txt
tracert tipsentrucs.nl >> registration.txt
Save the file, for example as networkcontrol.bat and run it. Be patient: a Command Prompt window will appear and the commands will be executed. The window will close automatically once all commands have been executed. Then open the file Registration.txt: the results of the audit are neatly documented.
PowerShell
In Windows 10, in addition to the 'classic' Command Prompt, you will also find another command line: PowerShell. What are the differences? You can think of PowerShell as the advanced brother of the Command Prompt. The component is mainly used within organizations by system administrators who can perform administrative tasks (of servers) with it, among other things. You can run more complex scripts with it. The Command Prompt is older than PowerShell. The average user does not need to use PowerShell. The Command Prompt is also sufficient for our purpose.
Tip 06: Overview
Now that we know how to use a script to write information to a text file, we can also use this tactic to create useful overviews. For example, if you want to know which files are in a folder. With the following script you automatically make an inventory of files in the folder D:\Tips and write this information to a file Overview.txt, which is placed in the same folder:
@ECHO OFF
:: Show all files in the Tips folder in the text file Overview
dir "D:\Tips" >> D:\Tips\Overview.txt
ECHO The inventory has been made
PAUSE
When finished, open the file Overview.txt to see the files.
Scripts allow you to perform tasks automaticallyTip 07: At startup
It becomes even more interesting if you have a script that has to be run every time Windows starts. First we create a shortcut to the script file. Right click on the script file and choose Copy to / Desktop (create shortcut). After that, open the desktop and check if the shortcut is present. Right click on it and choose To cut. Open the window To carry out (tip: use the key combination Windows key+R) and type Shell:startup, followed by pressing Enter. The map Startup will be opened. Right click in it and choose To stick. The shortcut to the script file is now present in the folder Startup. Shut down Windows by choosing . from the Start menu On/Off / Restart. From now on, the script will run automatically every time Windows starts. To end this, simply delete the file from the Startup folder.
Tip 08: System info
You can also write a script file if you want to see more information about the computer used. For example, which operating system is used, how much memory the computer has and which network is used. The script will then look like this:
@ECHO OFF
:: With this script you get information about the computer used
TITLE About This Computer
ECHO Please wait while we get information about the computer!
:: Step 1: Which Windows this computer is using
ECHO ============================
ECHO INFORMATION ABOUT WINDOWS
ECHO ============================
system info | findstr /c:"OS Name"
system info | findstr /c:"OS Version"
system info | findstr /c:"SystemType"
:: Step 2: What hardware does this computer use
ECHO ============================
ECHO INFORMATION ABOUT THE HARDWARE
ECHO ============================
system info | findstr /c:"Total Physical Memory"
wmic cpu get name
:: Step 3: Which network this computer is using
ECHO ============================
ECHO INFORMATION ABOUT THE NETWORK
ECHO ============================
ipconfig | findstr IPv4
ipconfig | findstr IPv6
PAUSE
Tip 09: Or to file
You can also use the script from tip 8 to generate a text file with the information about the computer. As you read in tip 4, the addition >> filename.txt used. With this knowledge you can expand the script further. Following is the first part of the script as an example:
@ECHO OFF
:: With this script you get information about the computer used
TITLE About This Computer
ECHO Please wait while we get information about the computer!
:: Step 1: Which Windows this computer is using
ECHO ============================
ECHO INFORMATION ABOUT WINDOWS
ECHO ============================
system info | findstr /c:"OS Name" >> Information.txt
system info | findstr /c:"OS Version" >> Information.txt
system info | findstr /c:"System Type" >> Information.txt