Batch Script - Process

Hello there, aspiring programmers! Today, we're going to dive into the exciting world of Batch scripting and explore how we can interact with processes on our Windows computers. Don't worry if you're new to programming – I'll guide you through each step with plenty of examples and explanations. Let's get started!

Batch Script - Process

Viewing the List of Running Processes

Before we can manage processes, we need to know what's running on our system. In Batch scripting, we have a handy command called tasklist that lets us see all the active processes.

Basic Usage

Let's start with the simplest form of the tasklist command:

@echo off
tasklist
pause

Save this as list_processes.bat and run it. You'll see a long list of processes, including their names, Process IDs (PIDs), memory usage, and more.

Filtering the Output

Sometimes, we want to find specific processes. We can use filters to narrow down our search:

@echo off
tasklist /fi "imagename eq notepad.exe"
pause

This script will only show instances of Notepad that are running. Replace notepad.exe with any process name you're interested in.

Outputting to a File

What if we want to save this information for later? We can redirect the output to a text file:

@echo off
tasklist > process_list.txt
echo Process list saved to process_list.txt
pause

Now you'll have a file named process_list.txt with all your running processes.

Killing a Particular Process

Sometimes, a program might freeze or you might need to stop it for other reasons. The taskkill command comes to our rescue!

Basic Usage

Here's how to kill a process by its name:

@echo off
taskkill /im notepad.exe
pause

This will close all instances of Notepad. Be careful with this power!

Killing by Process ID

For more precise control, we can kill a process by its PID:

@echo off
tasklist /fi "imagename eq notepad.exe" /fo csv /nh > temp.txt
for /f "delims=," %%a in (temp.txt) do set pid=%%~a
taskkill /pid %pid% /f
del temp.txt
pause

This script finds the PID of Notepad and then kills it. The /f flag forces the process to close.

Killing Multiple Processes

Let's say you want to close all web browsers:

@echo off
taskkill /f /im chrome.exe
taskkill /f /im firefox.exe
taskkill /f /im msedge.exe
echo All browsers should be closed now.
pause

Starting a New Process

Starting processes is just as important as stopping them. Let's look at how we can launch programs using Batch.

Basic Usage

To start a program, we simply use its name or path:

@echo off
start notepad.exe
echo Notepad should be open now.
pause

Starting with Arguments

We can also pass arguments to the programs we start:

@echo off
start notepad.exe "C:\example.txt"
echo Notepad should be open with example.txt.
pause

Starting Minimized or Maximized

Sometimes, we want to control how a program starts:

@echo off
start /min notepad.exe
echo Notepad should be open and minimized.
timeout /t 3
start /max calc.exe
echo Calculator should be open and maximized.
pause

The /min and /max switches control the window state.

Useful Process Management Commands

Here's a table summarizing the key commands we've learned:

Command Description Example
tasklist Lists running processes tasklist
taskkill Terminates a process taskkill /im notepad.exe
start Launches a new process start notepad.exe

Remember, with great power comes great responsibility. Always be cautious when terminating processes, as it might lead to data loss if you're not careful.

Conclusion

Congratulations! You've just taken your first steps into the world of process management with Batch scripting. We've covered how to view, kill, and start processes, which are fundamental skills for any budding Windows administrator or power user.

As you practice these commands, you'll find yourself becoming more comfortable with controlling your computer through scripts. It's like having a secret superpower – you can now manage your PC with just a few lines of code!

Remember, the best way to learn is by doing. Try creating your own scripts, experiment with different commands, and don't be afraid to make mistakes. That's how we all learn and grow as programmers.

Keep coding, keep exploring, and most importantly, have fun with it! Who knows? The next time your friend's computer is acting up, you might just be the hero they need with your newfound Batch scripting skills.

Happy scripting, and may your processes always run smoothly!

Credits: Image by storyset