I would like to block social medias from my PC according to time. Block after 9 PM and unblock after 11 AM (like parental control) I have tried following but failed.
Created a block.bat
file
@echo off
(Get-Content C:\Windows\System32\drivers\etc\hosts) | ForEach-Object { $_ -replace "#127.0.0.1 facebook.com", "127.0.0.1 facebook.com" } | Set-Content C:\Windows\System32\drivers\etc\hosts
Created a Unblock.bat
file
@echo off
(Get-Content C:\Windows\System32\drivers\etc\hosts) | ForEach-Object { $_ -replace "127.0.0.1 facebook.com", "#127.0.0.1 facebook.com" } | Set-Content C:\Windows\System32\drivers\etc\hosts
And Added these 2 bat file to Task Scheduler. Block will run at 11 AM and unlock at 9 PM.
I am getting this error from PowerShell:
Set-Content : The input object cannot be bound because it did not contain the information required to bind all mandatory parameters: Path At line:1 char:140 + ... e "#127.0.0.1 facebook.com", "127.0.0.1 facebook.com" } | Set-Content + ~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (# Copyright (c)...Microsoft Corp.:String) [Set-Content], ParameterBindingException + FullyQualifiedErrorId : InputObjectMissingMandatory,Microsoft.PowerShell.Commands.SetContentCommand
And manually running bat file does nothing.
Here is our solution then.
Unblock
Block
Give a shot for this code :
You cannot block facebook via hosts forwarding it to localhost.
Do it on your router or firewall.
But if works for you Go with batch file. Block
best use this vbs
https://www.experts-exchange.com/questions/26971010/VBScript-Hosts-File-add-remove-multiple-entries.html