Call or Text Us Anytime at: 845-592-4019 or at 315-240-3966

PowerShell Script to Reset Host Files

Here’s a PowerShell script to reset the Windows hosts file to its default state. The hosts file is located at C:\Windows\System32\drivers\etc\hosts and typically contains a default entry for localhost.

# Define the path to the hosts file
$hostsFilePath = "C:\Windows\System32\drivers\etc\hosts"

# Define the default content for the hosts file
$defaultHostsContent = @"
# 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
"@

# Check if the hosts file exists
if (Test-Path $hostsFilePath) {
    # Backup the current hosts file
    $backupFilePath = "$hostsFilePath.bak"
    Copy-Item -Path $hostsFilePath -Destination $backupFilePath -Force
    Write-Host "A backup of the current hosts file has been created at $backupFilePath"

    # Reset the hosts file content to default
    try {
        Set-Content -Path $hostsFilePath -Value $defaultHostsContent -Force
        Write-Host "The hosts file has been reset to its default state."
    } catch {
        Write-Host "Failed to reset the hosts file: $_"
    }
} else {
    Write-Host "Hosts file not found at $hostsFilePath."
}

Steps to run the script:

  1. Open PowerShell as Administrator.
  2. Paste the script into the PowerShell window or save it as a .ps1 file and run it.
  3. The script will:
  • Backup the current hosts file as hosts.bak.
  • Reset the hosts file to its default content.

This restores the default hosts file configuration, which typically includes only the localhost entries.

Call Now