If you work on computers, you probably do certain tasks over and over again. For example, when we update Windows 10, we download the media creation tool from Microsoft’s website and run it. Any repeated tasks should be automated. This article will go through some of my favorite scripting languages, some examples of their uses, and why you’d use one over the other.
Scripting languages allow you to automate reputative tasks. They are very similar to other programming languages but they are generally not compiled making them easier to add to the program.
PowerShell
We work on and manage a lot of Windows computers. The best way to automate tasks on Windows is via PowerShell. Newer versions of Windows include PowerShell right out of the box, which makes it much easier to use. Some use cases aren’t even for automation purposes. For example, creating a new user account in Windows 10 can be cumbersome as the Settings app feels clunky. There are PowerShell commands which allows you to add a new user account, give it a password, and even assign it to the administrator group. Powershell also supports basic graphical elements like an input box.
Create a new user called newuser
net user /add newuser
Set the password for newuser to password
net user newuser password
Add the newuser to the administrators group
net localgroup administrators /add newuser
Bash
We use Linux frequently here as well and bash is the standard shell. Bash is an acronym for Bourne Again Shell as it replaces the Bourne shell. Bash is extremely powerful as it has easy access to all Linux commands. Bash comes pre installed on most Linux distributions. Windows even supports Bash now with the Windows Subsystem for Linux (WSL). If you’re more familiar with Linux commands, you can use them to manipulate your Windows computer rather than using PowerShell. For instance, you can delete files in a folder if they’re older than a month. This could be useful if you’re using that folder as a backup destination but want to purge old backups to save space.
Delete files and folders older than 30 days in the backup directory under the C: drive
find "C:\backup" -mindepth 1 -maxdepth 1 -type d -mtime +30 -exec rm -rf {} +
Another example is if you need to find a specific word in a text document. You can use the cat command, yes cat – it’s short for concatenate, to view the entire contents of the file. To find a specific word, use the grep command. This will find specific characters and return all instances of those characters. In order to get the output from the cat file to the grep command, we need to use a pipe.
This command will take the contents of the txtdoc file and send that to the input of the grep command. Grep will then highlight all “a” characters in each line.
cat txtdoc | grep a
Python
If you need to do something more advanced on a Linux machine, Python is the way to go. Python is not just available for Linux, however, it’s available on all major platforms. The Linux community has adopted Python almost entirely, and as a result, most distributions come with Python preinstalled. Python allows you to run shell commands using a script. One of Python’s greatest strengths is how easy it is to manipulate strings. This is probably why the Linux community adopted it as all hardware on Linux can be manipulated and configured via text files.
This example executes a shell command in Python
import os
dirname = input("Enter folder name: ")
os.system("mkdir " + dirname)
print("Created a folder called " + dirname + " in the current directory")
First we need to import the os module. This allows us to execute shell commands. Next we are getting input from the user and assigning it to the variable “dirname”. Next we are creating a directory in whatever directory the script was run in, and giving it the name the user inputted. Finally, we are telling the user the new folder was created successfully.
Ruby
The last scripting language I’m going to cover is Ruby. Ruby is very similar to Python but it’s normally not found pre installed on Linux machines. Ruby’s hello world program is one line.
puts "Hello World!"
Ruby is famous for how easy the syntax is. Want to print something out 5 times? No problem.
5.times do
print 'This is printed 5 times '
end
Ruby supports classes just as Python does. The only language on this list which doesn’t support classes is Bash. Another term for this is object oriented programming.
There are many different programming languages. Scripting languages are languages which are suited for fast and easy program creation. They allow you to create one off or multi-use programs in a few minutes. If you work in IT, you probably already know the importance of knowing at least one of these languages. If you are an end user and are curious about scripting, I recommend giving Python a try. They can save a lot of time when you need to do a specific task over and over again.