windows shell script example

Mastering Windows Shell Scripts: Example Tutorials

By Anthony Thomas
AD

Windows Shell Scripting, a powerful tool for automating tasks and streamlining workflows, is a skill that every Windows user can benefit from. While it might seem daunting at first, especially for those new to coding, Windows Shell Scripting is actually quite accessible and can be learned with a bit of practice. Let's dive into the world of Windows Shell Scripting with some practical examples.

Before we get started, it's important to understand what Windows Shell Scripting is and why you might want to use it. In simple terms, Windows Shell Scripting is a way to automate repetitive tasks by writing a series of commands that can be executed with a single click. This can save you time and reduce errors, as you no longer have to manually perform each step every time you need to complete a task.

Getting Started with Windows Shell Scripting

To begin with Windows Shell Scripting, you'll need to open the Windows Command Prompt. You can do this by pressing the Windows key + R, typing "cmd" into the Run dialog box, and pressing Enter.

AD
an image of a blue screen with text
an image of a blue screen with text
Intro to Shell Scripts
Intro to Shell Scripts
How to Create/Write a Simple/Sample Linux Shell/Bash Script
How to Create/Write a Simple/Sample Linux Shell/Bash Script

Once you're in the Command Prompt, you can start typing commands. These commands can be as simple as "dir" to list the files in a directory, or as complex as a script that automates a series of tasks. To create a script, you'll need to open a text editor like Notepad and write your commands there.

Creating Your First Script

Let's start with a simple example: a script that lists the files in a specified directory. To create this script, open Notepad and type the following lines:

Master Windows PowerShell: Beginner Tutorial & Practical Examples πŸ’»
Master Windows PowerShell: Beginner Tutorial & Practical Examples πŸ’»
Bash if elif else Statement with Examples - Linux Shell Scripting Guide
Bash if elif else Statement with Examples - Linux Shell Scripting Guide
Windows PowerShell - DZone Refcards
Windows PowerShell - DZone Refcards
@echo off
dir C:\Users\YourUsername\Documents

Here's what this script does:

  • @echo off tells the Command Prompt not to display the commands as they're executed.
  • dir C:\Users\YourUsername\Documents lists the files in the specified directory.

Save this script with a .bat extension (e.g., list_files.bat). Now, whenever you double-click this file, it will run the script and list the files in your Documents folder.

AD

Adding User Input to Your Script

Let's make our script a bit more dynamic by adding user input. We'll modify the script to ask the user which directory they want to list the files from. Here's how you can do it:

What Is Windows PowerShell Scripting?
What Is Windows PowerShell Scripting?
PowerShell для Π½Π°Ρ‡ΠΈΠ½Π°ΡŽΡ‰ΠΈΡ…: ΠΊΠΎΠΌΠ°Π½Π΄Π»Π΅Ρ‚Ρ‹, скрипты, ΠΊΠΎΠ½Π²Π΅ΠΉΠ΅Ρ€
PowerShell для Π½Π°Ρ‡ΠΈΠ½Π°ΡŽΡ‰ΠΈΡ…: ΠΊΠΎΠΌΠ°Π½Π΄Π»Π΅Ρ‚Ρ‹, скрипты, ΠΊΠΎΠ½Π²Π΅ΠΉΠ΅Ρ€
a table that has some different types of words on it, including the names and abbreviations
a table that has some different types of words on it, including the names and abbreviations
Linux Shell Guide
Linux Shell Guide
Top 11 Resources to Learn Shell Scripting for Free
Top 11 Resources to Learn Shell Scripting for Free
All PowerShell Operators Explained
All PowerShell Operators Explained
@echo off
set /p directory=Enter the directory path:
dir %directory%

In this script, the set /p command prompts the user to enter a directory path, which is then stored in the variable directory. The script then lists the files in that directory.

Using Loops and Conditional Statements

Now that you've got the basics down, let's explore some more advanced features of Windows Shell Scripting. Loops and conditional statements can make your scripts much more powerful and flexible.

AD

For example, let's say you want to create a script that renames all the files in a directory with a specific extension (like .txt) to include the current date and time. Here's how you could do it using a loop:

Cool Stuff about PowerShell 5.0 in Windows 10 - Scripting Blog [archived]
Cool Stuff about PowerShell 5.0 in Windows 10 - Scripting Blog [archived]
the new office document window is open
the new office document window is open
I used Sophia Script to take back control of Windows 11, and I wish I did it sooner
I used Sophia Script to take back control of Windows 11, and I wish I did it sooner
Building Eclipse Projects with custom external Shell Scripts
Building Eclipse Projects with custom external Shell Scripts
Use Classic Shell to Get a Classic Start Menu & Explorer Toolbar in Windows 8
Use Classic Shell to Get a Classic Start Menu & Explorer Toolbar in Windows 8
How To Find Windows Experience Index scores in Windows 8.1
How To Find Windows Experience Index scores in Windows 8.1
Quick Tip: Scripting a Custom Tool Window in Maya | Envato Tuts+
Quick Tip: Scripting a Custom Tool Window in Maya | Envato Tuts+
Explore Window in Word 2010
Explore Window in Word 2010
How to Use Open-Shell in Windows 11
How to Use Open-Shell in Windows 11
the windows welcome music screen is shown in this screenshote image, which appears to be an open window
the windows welcome music screen is shown in this screenshote image, which appears to be an open window
an image of a computer screen with the words in spanish
an image of a computer screen with the words in spanish
Top 50 Linux Commands you must know with example - StudyPK
Top 50 Linux Commands you must know with example - StudyPK
an illustrated guide to the types and sizes of windows in different styles, sizes and colors
an illustrated guide to the types and sizes of windows in different styles, sizes and colors
@echo off
set "extension=.txt"
for %%f in (*%extension%) do (
    set "new_name=%%~nf_%date:~-4,4%%~xf"
    rename "%%f" "%new_name%"
)

Here's what this script does:

  • set "extension=.txt" sets the extension we're looking for.
  • for %%f in (*%extension%) do ( loops through each file with the specified extension.
  • set "new_name=%%~nf_%date:~-4,4%%~xf" creates a new name for the file that includes the current year.
  • rename "%%f" "%new_name%" renames the file to the new name.

Using Conditional Statements

Conditional statements allow your script to make decisions based on certain conditions. For example, you might want to create a script that backs up important files to a specified location, but only if those files have been modified in the last week. Here's how you could do it using an if statement:

AD
@echo off
set "source=C:\Users\YourUsername\Documents"
set "destination=E:\Backups"
for %%f in ("%source%\*") do (
    if "%%~tF" GEQ "%date:~10,10%" (
        xcopy "%%f" "%destination%" /s /i
    )
)

Here's what this script does:

  • set "source=C:\Users\YourUsername\Documents" and set "destination=E:\Backups" set the source and destination directories.
  • for %%f in ("%source%\*") do ( loops through each file in the source directory.
  • if "%%~tF" GEQ "%date:~10,10%" ( checks if the file was modified in the last week.
  • xcopy "%%f" "%destination%" /s /i copies the file to the backup location if the condition is true.

Remember, the key to effective Windows Shell Scripting is practice. The more you write and test scripts, the more comfortable you'll become with the syntax and the more efficient you'll become at automating your tasks.

So, what are you waiting for? Start exploring the world of Windows Shell Scripting today! Who knows, you might just find that it's not as scary as it seems. Happy scripting!

AD