Version: 2.0.0 Author: myTech.Today PowerShell Version: 7.0+ (PowerShell Core) Platforms: Windows, macOS, Linux
The hosts.ps1 script manages the system hosts file by downloading and merging ad-blocking/malware-blocking rules from someonewhocares.org while preserving existing custom host entries.
Cross-Platform Support:
- Windows:
C:\Windows\System32\drivers\etc\hosts - macOS:
/etc/hosts - Linux:
/etc/hosts
- Cross-Platform Compatibility - Works on Windows, macOS, and Linux
- Elevated Privilege Check - Ensures script runs with proper permissions (Administrator on Windows, root on macOS/Linux)
- Automatic Backups - Creates timestamped backups before making changes
- Smart Merging - Preserves custom host entries while adding blocking rules
- DNS Cache Flush - Automatically applies changes by flushing DNS cache (platform-specific)
- Comprehensive Logging - Follows myTech.Today standards with monthly log rotation
- WhatIf Support - Preview changes without applying them
- Restore Capability - Restore from any previous backup
- Error Handling - Robust error handling with detailed logging
- PowerShell 7.0 or later (PowerShell Core) - Install PowerShell
- Elevated privileges:
- Windows: Run PowerShell as Administrator
- macOS/Linux: Run with
sudo pwshor as root
- Internet connection (for downloading blocking rules)
- Copy
hosts.ps1to your desired location - Run PowerShell with elevated privileges:
- Windows: Run PowerShell as Administrator
- macOS/Linux: Run
sudo pwsh
- Execute the script with desired parameters
# Update hosts file with latest blocking rules
.\hosts.ps1
# Preview changes without applying (WhatIf)
.\hosts.ps1 -WhatIf
# Update without confirmation prompts
.\hosts.ps1 -Force# Create backup only (no update)
.\hosts.ps1 -BackupOnly
# Restore from most recent backup
.\hosts.ps1 -RestoreBackup
# Restore from specific backup
.\hosts.ps1 -RestoreBackup -BackupPath "C:\Users\YourName\myTech.Today\hosts\backups\hosts.backup.2025-11-21_143022"# Update without flushing DNS cache
.\hosts.ps1 -SkipDNSFlush
# Combine parameters
.\hosts.ps1 -Force -SkipDNSFlush| Parameter | Type | Description |
|---|---|---|
-BackupOnly |
Switch | Creates backup of current hosts file without updating |
-RestoreBackup |
Switch | Restores hosts file from most recent backup |
-BackupPath |
String | Specific backup file to restore (used with -RestoreBackup) |
-SkipDNSFlush |
Switch | Skip DNS cache flush after updating hosts file |
-Force |
Switch | Skip confirmation prompts |
-WhatIf |
Switch | Preview changes without applying them |
| Platform | Path |
|---|---|
| Windows | C:\Windows\System32\drivers\etc\hosts |
| macOS | /etc/hosts |
| Linux | /etc/hosts |
| Platform | Backups | Logs |
|---|---|---|
| Windows | %USERPROFILE%\myTech.Today\hosts\backups\ |
%USERPROFILE%\myTech.Today\logs\hosts.YYYY-MM.md |
| macOS/Linux | ~/myTech.Today/hosts/backups/ |
~/myTech.Today/logs/hosts.YYYY-MM.md |
- Platform Detection - Automatically detects Windows, macOS, or Linux
- Privilege Check - Verifies elevated privileges (Administrator or root)
- Backup Creation - Creates timestamped backup of current hosts file
- Download Rules - Fetches latest blocking rules from someonewhocares.org
- Extract Custom Entries - Identifies and preserves custom host entries
- Merge Content - Combines blocking rules with custom entries
- Update Hosts File - Writes merged content to system hosts file
- Flush DNS Cache - Clears DNS cache using platform-specific commands
The script automatically preserves custom host entries that are not part of the someonewhocares.org blocking list. Custom entries are identified by:
- Entries that don't contain "someonewhocares.org" references
- Entries in the custom section (marked by the script)
- User-added comments and configurations
Custom entries are appended to the end of the merged hosts file with a clear section marker:
# === Custom Host Entries (Preserved by myTech.Today hosts.ps1) ===
- Backups are created with timestamp format:
hosts.backup.YYYY-MM-DD_HHmmss - Maximum of 10 backups are kept (oldest are automatically deleted)
- Each backup operation logs the file path and size
- Backups can be restored at any time using the
-RestoreBackupparameter
All operations are logged following myTech.Today standards:
- Log Location:
%USERPROFILE%\myTech.Today\logs\hosts.YYYY-MM.md - Log Format: Markdown table format
- Log Rotation: Monthly (one file per month)
- Log Levels: INFO, WARN, ERROR
Example log entry:
| 2025-11-21 14:30:22 | [INFO] | Hosts file update completed successfully |The script includes comprehensive error handling:
- Network errors during download
- File permission errors
- Invalid backup paths
- DNS flush failures
- Detailed error logging with stack traces
0- Success1- Error (check logs for details)
# Windows: Run PowerShell as Administrator
.\hosts.ps1
# macOS/Linux: Run with sudo
sudo pwsh -File hosts.ps1# Update with latest blocking rules (no prompts)
.\hosts.ps1 -Force# Preview what would change
.\hosts.ps1 -WhatIf# Create backup before manually editing hosts file
.\hosts.ps1 -BackupOnly# Restore from most recent backup
.\hosts.ps1 -RestoreBackupWindows:
- Ensure you're running PowerShell as Administrator
- Check file permissions on the hosts file
macOS/Linux:
- Run with
sudo pwshor as the root user - Check file permissions:
ls -la /etc/hosts - Reset permissions if needed:
sudo chmod 644 /etc/hosts
- Verify internet connection
- Check if someonewhocares.org is accessible
- Review firewall/proxy settings
- Check log file for details on what was detected
- Ensure custom entries don't contain "someonewhocares.org" references
- Manually add entries after update if needed
Some Linux distributions don't have a DNS cache by default. If DNS flush fails:
- The script will continue without error
- Changes will take effect on the next DNS lookup
- Try restarting the network service if needed
For issues, questions, or contributions:
- GitHub: https://github.com/mytech-today-now/PowerShellScripts
- Check logs:
- Windows:
%USERPROFILE%\myTech.Today\logs\hosts.YYYY-MM.md - macOS/Linux:
~/myTech.Today/logs/hosts.YYYY-MM.md
- Windows:
Part of the myTech.Today PowerShell Scripts collection.
- Cross-platform support for Windows, macOS, and Linux
- Platform-specific hosts file path detection using
$IsWindows,$IsMacOS,$IsLinux - Cross-platform elevated privilege detection (Administrator on Windows, root on macOS/Linux)
- Platform-specific DNS cache flush commands
- Updated documentation for multi-platform usage
- Requires PowerShell 7.0+ (PowerShell Core)
- Initial release (Windows-only)
- Download and merge blocking rules from someonewhocares.org
- Preserve custom host entries
- Automatic backup management
- DNS cache flushing
- Comprehensive logging
- WhatIf support
- Restore capability