-2

want to make sure this code will remove a specific website from hosts file, unblocking it.

pushd %SystemRoot%\system32\drivers\etc\hosts
copy hosts hosts.bak
findstr /v /c:"drive.google.com" hosts.bak > hosts
popd

And if not then what I can use in a batch file to remove specific websites from hosts file.

SomethingDark
  • 10,902
  • 5
  • 44
  • 47
keagz
  • 5
  • 1
  • 3
    ...what happened when you tried it? – SomethingDark Nov 01 '16 at 01:26
  • `findstr /v /c:"drive.google.com" %SystemRoot%\system32\drivers\etc\hosts > %SystemRoot%\system32\d rivers\etc\hosts` is all you need. 4 lines into one line. –  Nov 01 '16 at 02:00
  • I didn't try it yet as I am still pretty new to this and don't wanna just use commands I'm not sure about. But thanks i will try that :) – keagz Nov 01 '16 at 03:05
  • You can take a look at this ==> [How to block social medias from windows using hosts file](http://stackoverflow.com/questions/38221819/how-to-block-social-medias-from-windows?answertab=active#tab-top) – Hackoo Nov 01 '16 at 08:42

1 Answers1

0

If in doubt about potentially removing something then the following code, which utilises powershell from within your batch file, will just comment out any matching lines. (Edit SiteList as appropriate).

@Echo Off
Set SiteList="drive.google.com" "microsoft.com" "dostips.com" "stackoverflow.com"
For %%A In (%Sitelist%) Do Call :Sub %%A
Exit/B
:Sub
@Powershell "(Get-Content """$($env:windir)\System32\drivers\etc\hosts""") -replace ('^\s*127.0.0.1\s*%~1','#127.0.0.1 %~1') | Out-File """$($env:windir)\System32\drivers\etc\hosts"""  -Force"

Because the hosts file is located in a protected area of your system you will need to Run as administrator.

The method is as used in the link kindly provided by Hackoo in the comments section, but due to your batch-file tag and your stated lack of knowledge I have provided this as an alternative.

It is also worth noting that some hosts use 0.0.0.0 instead of 127.0.0.1, so you may need to adjust the code accordingly.

Compo
  • 30,301
  • 4
  • 20
  • 32