Virajsinh

Windows Host File Entry Using .bat File

Oct 10th, 2025 (edited)
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 0.63 KB | Source Code | 0 0
  1. @echo off
  2. :: Check for administrative privileges
  3. net session >nul 2>&1
  4. if %errorLevel% neq 0 (
  5.     echo This script requires administrator privileges. Please run as administrator.
  6.     powershell -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
  7.     exit /b
  8. )
  9.  
  10. set "HOSTS_FILE=%SystemRoot%\System32\drivers\etc\hosts"
  11. set "ENTRY=192.168.1.200 gpcl.local"
  12.  
  13. :: Check if exact line already exists
  14. findstr /R /C:"^%ENTRY%$" "%HOSTS_FILE%" >nul
  15. if %errorLevel% equ 0 (
  16.     echo Entry already exists in hosts file.
  17. ) else (
  18.     echo Adding entry to hosts file...
  19.     >>"%HOSTS_FILE%" echo.%ENTRY%
  20.     echo Entry added.
  21. )
  22.  
  23. pause
  24.  
Advertisement
Add Comment
Please, Sign In to add comment