# PCS Toolkit - Hosts File Manager # View, backup, and edit the Windows hosts file $timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm-ss" $logFile = "$env:USERPROFILE\Desktop\HostsFile_$timestamp.txt" $hostsPath = "$env:SystemRoot\System32\drivers\etc\hosts" $backupPath = "$env:USERPROFILE\Desktop\hosts_backup_$timestamp.txt" function Log($msg) { Write-Host $msg Add-Content $logFile $msg } Log "========================================" Log " PCS Toolkit - Hosts File Manager" Log "========================================" Log "Generated: $(Get-Date)" Log "Computer: $env:COMPUTERNAME" Log "" # Check if running as admin $isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) if (-not $isAdmin) { Write-Host "WARNING: Not running as Administrator. Editing will require elevation." -ForegroundColor Yellow Log "[!] Not running as Administrator" } Log "" Log "Hosts file location: $hostsPath" Log "" # Display current hosts file Log "=== CURRENT HOSTS FILE CONTENTS ===" Log "" if (Test-Path $hostsPath) { $content = Get-Content $hostsPath -EA SilentlyContinue $lineNum = 0 foreach ($line in $content) { $lineNum++ if ($line -match '^\s*\d' -and $line -notmatch '^\s*#') { Write-Host ("{0,3}: {1}" -f $lineNum, $line) -ForegroundColor Green } else { Write-Host ("{0,3}: {1}" -f $lineNum, $line) } Log ("{0,3}: {1}" -f $lineNum, $line) } } else { Log "ERROR: Hosts file not found!" } Log "" Log "=== ACTIVE ENTRIES (Non-commented) ===" $activeEntries = $content | Where-Object { $_ -match '^\s*\d' -and $_ -notmatch '^\s*#' } if ($activeEntries) { $activeEntries | ForEach-Object { Log " $_" } } else { Log " (No active custom entries)" } function Show-Menu { Write-Host "" Write-Host "=== HOSTS FILE MENU ===" -ForegroundColor Cyan Write-Host "" Write-Host " 1. View hosts file" Write-Host " 2. Create backup" Write-Host " 3. Add entry" Write-Host " 4. Remove entry (comment out)" Write-Host " 5. Block a domain (redirect to 0.0.0.0)" Write-Host " 6. Add common ad-blocking entries" Write-Host " 7. Reset to Windows default" Write-Host " 8. Open hosts file in Notepad" Write-Host " 9. Exit" Write-Host "" } function Backup-HostsFile { Copy-Item $hostsPath $backupPath -Force Log "Backup created: $backupPath" Write-Host "Backup saved to: $backupPath" -ForegroundColor Green } function Add-HostsEntry { $ip = Read-Host "Enter IP address (e.g., 192.168.1.100)" $hostname = Read-Host "Enter hostname (e.g., myserver.local)" if ($ip -and $hostname) { $tab = [char]9 $entry = "$ip$tab$hostname" Log "Adding entry: $entry" try { Add-Content $hostsPath $entry -EA Stop Log "Entry added successfully" Write-Host "Entry added!" -ForegroundColor Green } catch { Log "ERROR: Failed to add entry. Run as Administrator." Write-Host "ERROR: Run this script as Administrator to modify hosts file." -ForegroundColor Red } } } function Block-Domain { $domain = Read-Host "Enter domain to block (e.g., ads.example.com)" if ($domain) { $tab = [char]9 $entry = "0.0.0.0$tab$domain" Log "Blocking domain: $domain" try { Add-Content $hostsPath $entry -EA Stop Log "Domain blocked successfully" Write-Host "Domain blocked!" -ForegroundColor Green } catch { Log "ERROR: Failed to block domain. Run as Administrator." Write-Host "ERROR: Run this script as Administrator to modify hosts file." -ForegroundColor Red } } } function Add-AdBlocking { $tab = [char]9 $adDomains = @( "0.0.0.0${tab}ads.google.com", "0.0.0.0${tab}pagead2.googlesyndication.com", "0.0.0.0${tab}ad.doubleclick.net", "0.0.0.0${tab}www.googleadservices.com", "0.0.0.0${tab}track.facebook.com", "0.0.0.0${tab}pixel.facebook.com" ) Write-Host "This will add the following entries:" -ForegroundColor Yellow $adDomains | ForEach-Object { Write-Host " $_" } $confirm = Read-Host "Continue? (y/n)" if ($confirm -eq 'y') { try { Add-Content $hostsPath "# PCS Toolkit - Ad Blocking Entries" $adDomains | ForEach-Object { Add-Content $hostsPath $_ } Log "Ad-blocking entries added" Write-Host "Ad-blocking entries added!" -ForegroundColor Green } catch { Write-Host "ERROR: Run as Administrator." -ForegroundColor Red } } } function Reset-HostsFile { $confirm = Read-Host "This will reset hosts file to Windows default. Continue? (y/n)" if ($confirm -eq 'y') { Backup-HostsFile $defaultHosts = @" # Copyright (c) 1993-2009 Microsoft Corp. # # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. # # This file contains the mappings of IP addresses to host names. Each # entry should be kept on an individual line. The IP address should # be placed in the first column followed by the corresponding host name. # The IP address and the host name should be separated by at least one # space. # # Additionally, comments (such as these) may be inserted on individual # lines or following the machine name denoted by a '#' symbol. # # For example: # # 102.54.94.97 rhino.acme.com # source server # 38.25.63.10 x.acme.com # x client host # localhost name resolution is handled within DNS itself. # 127.0.0.1 localhost # ::1 localhost "@ try { Set-Content $hostsPath $defaultHosts -Force Log "Hosts file reset to default" Write-Host "Hosts file reset to Windows default!" -ForegroundColor Green } catch { Write-Host "ERROR: Run as Administrator." -ForegroundColor Red } } } # Interactive menu do { Show-Menu $choice = Read-Host "Select option" switch ($choice) { "1" { Write-Host "" Get-Content $hostsPath | ForEach-Object { Write-Host $_ } } "2" { Backup-HostsFile } "3" { Add-HostsEntry } "4" { $lineToComment = Read-Host "Enter line number to comment out" if ($lineToComment -match '^\d+$') { $lines = Get-Content $hostsPath $idx = [int]$lineToComment - 1 if ($idx -ge 0 -and $idx -lt $lines.Count) { if ($lines[$idx] -notmatch '^\s*#') { $lines[$idx] = "# " + $lines[$idx] try { Set-Content $hostsPath $lines Log "Line $lineToComment commented out" Write-Host "Line commented out!" -ForegroundColor Green } catch { Write-Host "ERROR: Run as Administrator." -ForegroundColor Red } } else { Write-Host "Line is already a comment." -ForegroundColor Yellow } } } } "5" { Block-Domain } "6" { Add-AdBlocking } "7" { Reset-HostsFile } "8" { Start-Process notepad.exe $hostsPath -Verb RunAs -EA SilentlyContinue Log "Opened hosts file in Notepad (elevated)" } } } while ($choice -ne "9") Log "" Log "========================================" Log "SESSION COMPLETE" Log "========================================" explorer.exe "/select,$logFile"