10 useful batch script examples for Windows

Estimated Reading Time: 3 min

Rate this post

Batch Scripting for Windows

Batch scripting is used in Windows to automate tasks via .bat (batch) files, executed in the Command Prompt (cmd.exe). Below are 10 useful batch script examples for Windows.


1. Hello World (Basic Script)

@echo off
echo Hello, World!
pause
  • @echo off hides commands from being displayed.
  • echo prints text to the screen.
  • pause waits for user input before closing.

2. Create a Folder

@echo off
mkdir C:\Users\%USERNAME%\Desktop\NewFolder
echo Folder Created!
pause
  • mkdir creates a directory.
  • %USERNAME% dynamically gets the logged-in user.

3. Delete a Folder

@echo off
rd /s /q C:\Users\%USERNAME%\Desktop\NewFolder
echo Folder Deleted!
pause
  • rd /s /q removes a folder forcefully.

4. Check Internet Connection

@echo off
ping www.google.com -n 1 >nul
if errorlevel 1 (
    echo No Internet Connection
) else (
    echo Connected to the Internet
)
pause
  • ping checks connectivity.
  • if errorlevel detects connection failure.

5. Shutdown or Restart Computer

@echo off
echo Choose an option:
echo 1. Shutdown
echo 2. Restart
set /p choice="Enter option: "
if "%choice%"=="1" shutdown /s /t 0
if "%choice%"=="2" shutdown /r /t 0
pause
  • shutdown /s /t 0 shuts down immediately.
  • shutdown /r /t 0 restarts immediately.

6. Check System Information

@echo off
systeminfo | findstr /B /C:"OS Name" /C:"OS Version"
pause
  • systeminfo retrieves system details.
  • findstr filters the output.

7. Copy Files from One Folder to Another

@echo off
xcopy C:\SourceFolder C:\DestinationFolder /E /I /Y
echo Files Copied!
pause
  • xcopy copies files with subdirectories.
  • /E copies all subdirectories (even empty ones).
  • /I assumes the destination is a directory.
  • /Y suppresses overwrite prompts.

8. Delete Temporary Files (Clean Up)

@echo off
del /s /q C:\Windows\Temp\*.*
echo Temp files deleted!
pause
  • del /s /q removes all files inside the directory silently.

9. Open a Website in Browser

@echo off
start https://www.google.com
  • start opens a website using the default browser.

10. Endless Loop (Useful for Continuous Tasks)

@echo off
:loop
echo This runs in a loop!
timeout /t 3 /nobreak >nul
goto loop
  • :loop defines a label.
  • goto loop repeats execution.
  • timeout adds a delay.

Bonus


Automate System Cleanup

@echo off
cleanmgr /sagerun:1
echo Cleanup completed.
pause

List All Installed Programs

@echo off
wmic product get name,version
pause

Disable USB Ports for Security

@echo off
reg add HKLM\SYSTEM\CurrentControlSet\Services\USBSTOR /v Start /t REG_DWORD /d 4 /f
echo USB ports disabled.
pause

How to Run a Batch Script

  1. Open Notepad.
  2. Copy and paste the desired script.
  3. Save it as script.bat (with .bat extension).
  4. Double-click the file to run it.

Would you like a specific batch script for something else? 😊

Photo of author

Flora

10 useful batch script examples for Windows

Published

Update

I am Flora, the publisher and founder of *Be-Smart*, a platform dedicated to sharing insights and inspiration for living a fulfilling life. With a strong background in the web, my goal is to empower people to genuinely recognize and celebrate admirable actions big or small in themselves and others.

Leave a Comment