Operational Procedures · Topic 7 of 9
Scripting Basics
Recognise batch, PowerShell, VBScript, shell, JavaScript and Python scripts, basic constructs such as loops and variables, and the risks of scripts.
- 4 min
.batis a Windows batch file,.ps1is PowerShell and.vbsis VBScript..shis a Linux or macOS shell script..pyis Python and.jsis JavaScript.- Scripts automate tasks such as drive mapping, installs and backups.
What it is
A script is a text file of commands that a computer runs in order. Scripts automate repeated tasks, such as mapping drives, installing software or collecting system information. A technician does not need to write large programs, but must recognise script types, read simple scripts and know the risks.
How it works
Script file types
| Extension | Language | Platform and notes |
|---|---|---|
.bat | Batch file | Windows Command Prompt commands |
.ps1 | PowerShell | Windows, and also available on other systems. Powerful and object based |
.vbs | VBScript (Visual Basic Script) | Older Windows scripting |
.sh | Shell script | Linux and macOS |
.js | JavaScript | Web pages, and scripting in other environments |
.py | Python | Cross platform, readable, widely used for automation |
On Windows, the file extension tells the operating system which interpreter to use. A shell script on Linux often starts with a line such as #!/bin/bash, which names the shell.
Common uses
- Restarting machines or services.
- Remapping network drives at sign in.
- Installing applications and updates in bulk.
- Automating backups.
- Gathering system information.
- Starting scheduled maintenance tasks.
Basic building blocks
Comments are notes for people that the computer ignores. Batch uses REM, shell and PowerShell and Python use #, and VBScript uses an apostrophe.
Variables hold values. Data types include numbers (integers) and text (strings). Environment variables hold system values such as the user's folder.
Conditions run a command only if a test is true. Loops repeat commands for each item or until a condition changes.
Examples
Batch file (Windows Command Prompt):
@echo off
REM Map a drive and copy a file
net use Z: \\server\share
copy C:\Reports\daily.txt Z:\
PowerShell:
$name = "report"
if (Test-Path "C:\Temp\$name.txt") { Write-Output "found" } else { Write-Output "missing" }
Linux shell:
#!/bin/bash
# List log files
for f in *.log; do
echo "$f"
done
In the PowerShell example, $name is a variable and the if is a condition. In the shell example, the for is a loop. The variable $HOME is an environment variable in Linux and macOS, %USERPROFILE% is one in the Windows Command Prompt, and $env:USERPROFILE is the PowerShell form.
Running a script
In Windows, a batch file can be run by double clicking it or by typing its name in a Command Prompt. PowerShell has an execution policy, which controls whether scripts are allowed to run, and it is a safety setting. In Linux and macOS, a script must have the execute permission, set with chmod +x script.sh, and is run with ./script.sh.
Risks and good practice
| Risk | Meaning |
|---|---|
| Unintentionally introducing malware | A script from an unknown source can contain harmful commands |
| Inadvertently changing system settings | A mistake can delete files or alter configuration for many machines |
| Browser or system crashes from mishandling resources | A bad loop or heavy task can use up memory or processor time |
Read a script before you run it. Test on a single non critical machine first. Keep scripts in version control or a known location, and run them with the least privilege that works.
Never run a script you do not understand with administrator rights. It can change every machine it touches.
What to remember for the exam
.batis a Windows batch file,.ps1is PowerShell and.vbsis VBScript..shis a Linux or macOS shell script..pyis Python and.jsis JavaScript.- Scripts automate tasks such as drive mapping, installs and backups.
- Basic parts are comments, variables, data types, conditions, loops and environment variables.
- A shell script needs the execute permission on Linux and macOS.
- PowerShell has an execution policy that controls script running.
- Risks include malware, accidental setting changes and resource crashes.
- Test scripts before wide deployment.
Common mistakes
Running unknown scripts. A script can do anything the user can do.
Skipping the test. An error can spread across many computers.
Mixing up script types. A batch file will not run in a Linux shell.
Forgetting the execute permission. A Linux script without it will not start.
Check yourself
Which file extension is a PowerShell script?
.ps1.What is a loop?
A construct that repeats commands for each item or until a condition changes.
Name one risk of running a script from the internet.
It may contain malware, or it may change system settings unexpectedly.
Topic 7 of 9 in Operational Procedures
Test yourself on Operational Procedures
The full CompTIA A+ 220-1202 question bank has 835 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by CompTIA. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.