Block Websites in Windows XP, 7, 8 with Batch Script

This is a simple Batch Script which blocks Websites URL By editing 'Hosts' file. It also consists some functionality like saw previous blocked URLs, restore original hosts file or create new one etc.



1:  :: Site Blocker Script  
2:
3:  :: http://sec-articles.blogspot.com  
4:  :Start  
5:  @echo off & setlocal ENABLEEXTENSIONS  
6:  title Site Blocker  
7:  color a  
8:  mode con cols=80 lines=25  
9:  echo.  
10:  echo ===============================================================================  
11:  echo                     # Site Blocker #  
12:  echo.  
13:  echo ===============================================================================  
14:  echo.  
15:  echo Options :  
16:  echo.  
17:  echo 1) Block A Site  
18:  echo 2) Unblock All Blocked Sites  
19:  echo 3) Show Previous Blocked Sites  
20:  echo 4) About  
21:  echo 5) Exit   
22:  echo.  
23:  set /p ch="Enter Your Choice : "  
24:  if %ch%==1 goto Block  
25:  if %ch%==2 goto Restore  
26:  if %ch%==3 goto Show  
27:  if %ch%==4 goto About  
28:  if %ch%==5 goto End  
29:  echo.   
30:  echo Please Enter Correct Choice   
31:  pause > nul  
32:  goto Start  
33:  :Block  
34:  echo.  
35:  set /p url="Enter The URL : "  
36:  if exist "%systemroot%" (   
37:  if not exist "%systemroot%\System32\drivers\etc\hosts.bak" ( copy "%systemroot%\System32\drivers\etc\hosts" "%systemroot%\System32\drivers\etc\hosts.bak" )  
38:  echo 127.0.0.1  %url% >> "%systemroot%\System32\drivers\etc\hosts"  
39:  echo.   
40:  echo Url Blocked SuccessFully.!!  
41:  pause > nul  
42:  goto Start   
43:  ) else (  
44:  echo.  
45:  echo.  
46:  echo Error ?? Does not found Windows Directory.!!   
47:  pause > nul  
48:  goto Start )  
49:  :show  
50:  echo.  
51:  if not exist "%systemroot%\System32\drivers\etc\hosts" (   
52:  echo Error ?? Does Not found the Host File !!   
53:  pause > nul  
54:  goto Start )   
55:  @echo off & setlocal ENABLEEXTENSIONS  
56:  for /f "tokens=2" %%a in ('findstr /B 127.0.0.1 %systemroot%\System32\drivers\etc\hosts') do ( echo %%a & set d=%%a)   
57:  if exist %d%==nul ( echo You have not blocked any site..!! )   
58:  pause > nul  
59:  goto Start   
60:  :Restore  
61:  if exist "%systemroot%\System32\drivers\etc\hosts.bak" (   
62:  del "%systemroot%\System32\drivers\etc\hosts"  
63:  copy "%systemroot%\System32\drivers\etc\hosts.bak" "%systemroot%\System32\drivers\etc\hosts"   
64:  echo.  
65:  echo hosts File is SuccessFully Restored.!!!  
66:  pause > nul  
67:  goto Start )  
68:  echo.  
69:  echo Original host backup File are not Found.!!  
70:  echo Press Any key to Create new host File ...  
71:  pause > nul  
72:  echo # Copyright (c) 1993-2009 Microsoft Corp. > "%systemroot%\System32\drivers\etc\hosts"  
73:  echo # >> "%systemroot%\System32\drivers\etc\hosts"  
74:  echo # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. >> "%systemroot%\System32\drivers\etc\hosts"  
75:  echo # >> "%systemroot%\System32\drivers\etc\hosts"  
76:  echo # This file contains the mappings of IP addresses to host names. Each >> "%systemroot%\System32\drivers\etc\hosts"  
77:  echo # entry should be kept on an individual line. The IP address should >> "%systemroot%\System32\drivers\etc\hosts"  
78:  echo # be placed in the first column followed by the corresponding host name. >> "%systemroot%\System32\drivers\etc\hosts"  
79:  echo # The IP address and the host name should be separated by at least one >> "%systemroot%\System32\drivers\etc\hosts"  
80:  echo # space. >> "%systemroot%\System32\drivers\etc\hosts"  
81:  echo # >> "%systemroot%\System32\drivers\etc\hosts"  
82:  echo # Additionally, comments (such as these) may be inserted on individual >> "%systemroot%\System32\drivers\etc\hosts"  
83:  echo # lines or following the machine name denoted by a '#' symbol. >> "%systemroot%\System32\drivers\etc\hosts"  
84:  echo # >> "%systemroot%\System32\drivers\etc\hosts"  
85:  echo # For example: >> "%systemroot%\System32\drivers\etc\hosts"  
86:  echo # >> "%systemroot%\System32\drivers\etc\hosts"  
87:  echo #   102.54.94.97   rhino.acme.com     # source server >> "%systemroot%\System32\drivers\etc\hosts"  
88:  echo #    38.25.63.10   x.acme.com       # x client host >> "%systemroot%\System32\drivers\etc\hosts"  
89:  echo #   >> "%systemroot%\System32\drivers\etc\hosts"  
90:  echo # localhost name resolution is handled within DNS itself. >> "%systemroot%\System32\drivers\etc\hosts"  
91:  echo #     127.0.0.1    localhost >> "%systemroot%\System32\drivers\etc\hosts"  
92:  echo.  
93:  echo hosts File is SuccessFully Created..!!  
94:  pause > nul  
95:  goto Start   
96:  :About  
97:  echo.  
98:  echo This is a Simple Batch Script which edit the host file   
99:  echo located in "C:\Windows\System32\drivers\etc" directory.  
100:  echo Use this Script at your own risk, i am not responsible for  
101:  echo any kind of damage on your system.  
102:  echo Tested on Win XP, 7, 8  
103:  echo in windows 7, 8 run it with Admin privilege  
104:  echo.  
105:  pause  
106:  goto Start  
107:  :End  
108:  exit /b  



Download script from here :

How  To Video Tutorial :