Advertisement
hl2guide

Windows HOSTS File Updater 1.0B - PowerShell

May 16th, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Updates the Windows HOSTS file for the best protection from scams and unwanted content
  2. # [Must be run as an Administrator]
  3. # Version: 1.0B
  4. # Date Modified: 30/07/17
  5. # Lists at: https://github.com/StevenBlack/hosts
  6.  
  7. $sourceURL = 'https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/fakenews-social/hosts'
  8. $hostsFile = "$($env:windir)\system32\Drivers\etc\hosts"
  9. $outputFile = "$home\Desktop\HOSTS Backups\new-hosts-file.txt"
  10. $hostsBackupFile = "$home\Desktop\HOSTS Backups\"
  11.  
  12. # 1) Backs up current Windows HOSTS File to the Desktop
  13. Write-Host '1) Backing up Windows HOSTS file...' -ForegroundColor Yellow
  14. # Create Backup folder
  15. New-Item -Path $hostsBackupFile -ItemType directory -WarningAction SilentlyContinue -ErrorAction SilentlyContinue
  16. # Create Backup file
  17. $hostsBackupFile = $hostsBackupFile+'HOSTS-File-Backup-'+(Get-Date -Format "yyyy-MM-dd\@h-mm-ss")+'.zip'
  18. Compress-Archive -LiteralPath $hostsFile -CompressionLevel Optimal -DestinationPath $hostsBackupFile
  19. Write-Host 'Windows HOSTS file backed up.' -ForegroundColor Green
  20.  
  21. Write-Host
  22.  
  23. # 2) Downloads HOSTS file content
  24. Write-Host '2) Downloading latest HOSTS file update...' -ForegroundColor Cyan
  25. $start_time = Get-Date
  26. (New-Object System.Net.WebClient).DownloadFile($sourceURL, $outputFile)
  27. Write-Host 'HOSTS file update has been downloaded.' -ForegroundColor Green
  28. Write-Host "Time taken to download: $((Get-Date).Subtract($start_time).Seconds) second(s)" -ForegroundColor Green
  29.  
  30. Write-Host
  31.  
  32. # Checks Downloaded File
  33. if(Test-Path($outputFile))
  34. {
  35.     # Checks the filesize is larger than 1 MB
  36.     if((Get-Item $outputFile).length -gt 1mb)
  37.     {
  38.         # 3) Replace Windows HOSTS file
  39.         Write-Host '3) Updating Windows HOSTS file with new data...' -ForegroundColor Magenta
  40.         $newHOSTSData = Get-Content -Path $outputFile
  41.         Set-Content -Path $hostsFile -Value $newHOSTSData
  42.         # Clear the DNS
  43.         Clear-DnsClientCache
  44.         $nowDateTime = Get-Date
  45.         Write-Host 'Windows HOSTS has been updated, DNS cache cleared. Process completed at'$nowDateTime -ForegroundColor Green
  46.     }
  47. }
  48.  
  49. # Opens HOSTS file as Administrator
  50. Start-Process "notepad.exe" -ArgumentList "/W $hostsFile" -Verb runAs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement