Hosts: It'd be cool to have a PowerShell script to pull the latest copy periodically.

Created on 1 Oct 2015  路  8Comments  路  Source: StevenBlack/hosts

Windows enhancement

Most helpful comment

Closing this now. This is an old issue, and Windows is too fragmented. Folks are of course free to automate their own workflows.

All 8 comments

Feel free to submit one!

The permanent location is
https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts

Something like this one? ;-)
https://github.com/gaenserich/hostsblock

Here you go.

$hostsRequest = (Invoke-Webrequest -Uri "https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts")
$currentDate = ((get-date).ToUniversalTime()).ToString("dd-MM-yyyy hh-mm-ss")
$filePath = "C:\Windows\System32\drivers\etc\host-{0}.{1}"

if($hostsRequest.StatusCode -eq 200){
    $captureDomainRegex = "^(?:\d{1}\.\d{1}\.\d{1}.\d{1} )(.+)"
    $hosts = [PsCustomObject]@{unique=@(); duplicate=@();}

    $hostsRequest.Content | out-file -filepath ($filePath -f $currentDate, "txt")

    foreach ($line in Get-Content ($filePath -f $currentDate, "txt")){
        if($line -match $captureDomainRegex) {
            if($hosts.unique -contains $matches[1]){
                $hosts.duplicate += $matches[1]
            }else{
                $hosts.unique += $matches[1]
            }
        }
    }
    ConvertTo-Json $hosts | out-file -filepath ($filePath -f $currentDate, "json")
}

#load json and get the count of unique entries 
#(Get-Content ($filePath -f $currentDate, "json") | ConvertFrom-Json).unique.Count

This would create host.txt wherever your path is set to, then it will read that file and create
host.json with the following structure

{
    "unique":  [
                   "0koryu0.easter.ne.jp",
                   "1-atraffickim.tf",
                   "10-trafficimj.tf",
                   "109-204-26-16.netconnexion.managedbroadband.co.uk",
                   "11-atraasikim.tf",
                   "11.lamarianella.info",
                   "12-tgaffickvcmb.tf",
                   "13-ctscfficim.tf",
                   "alsoknowsit.com"
               ],
    "duplicate":  [
                      "11-atraasikim.tf",
                      "11.lamarianella.info"
                  ]
}

Gist

This one seems to work for me. You can run it with task scheduler for periodically updates.

# Create temp folder..
New-Item -ItemType Directory -Force -Path 'C:\Hosts\'
Set-Location C:\Hosts\
wget 'https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts' -OutFile hosts
# Clean up and remove dublicates ..
Get-Content hosts | Where { $_ -notmatch "\#" } | Where {$_.trim() -ne " " } | Sort-Object -Unique | Set-Content final
# Back-up and Update hostsfile..
Copy-Item -Recurse "C:\Windows\System32\drivers\etc\hosts" -Destination "C:\Windows\System32\drivers\etc\hosts.bak"
Copy-Item -Recurse ".\final" -Destination "C:\Windows\System32\drivers\etc\hosts"
# FlushDNS..
CMD.EXE /C "ipconfig /flushdns" 
Write-Host "`nHosts File Updated" -ForegroundColor Green
rm .\final -Recurse

Gist

$hosts = "$env:windirSystem32driversetchosts"
$hosts_custom = "C:UsersXXXXXXOneDrivehosts.custom.txt"

Invoke-WebRequest -Uri https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts -OutFile $hosts
if (Test-Path -Path $hosts_custom) {Get-Content $hosts_custom | Add-Content -Path $hosts}
C:WINDOWSsystem32cmd.exe /C C:WINDOWSsystem32ipconfig /flushdns

Does it count if it's not PowerShell?

https://github.com/ScriptTiger/Unified-Hosts-AutoUpdate

@sn0wflake Invoke-WebRequest works only from a specific version of powershell and I have seen that on Windows Server 2008 it doesn't by default.

Closing this now. This is an old issue, and Windows is too fragmented. Folks are of course free to automate their own workflows.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bigdargon picture bigdargon  路  3Comments

The-Compiler picture The-Compiler  路  3Comments

gadgetchnnel picture gadgetchnnel  路  3Comments

Sego1234 picture Sego1234  路  3Comments

beerisgood picture beerisgood  路  3Comments