You have likely faced this scenario: You are trying to point a local domain to your test server, you open the hosts file, make your changes, and hit save. Then, Windows hits you with the annoying Access Denied error. It happens to the best of us.

Whether you are setting up local staging environments or trying to block a specific site, editing the hosts file on Windows 10 or 11 requires a bit more than just double-clicking a text file. Windows protects this file aggressively because it controls how your computer maps hostnames to IP addresses.

In this guide, I will show you the three fastest ways to bypass these restrictions—ranging from a simple PowerShell one-liner for the pros to a modern GUI method for those who prefer a visual interface.

  • File Path: C:\Windows\System32\drivers\etc\hosts
  • Difficulty: Easy
  • Time Required: 2 Minutes
  • Permissions: Administrator Access Required

Method 1: The One-Liner PowerShell Command (Fastest)

If you are comfortable with the command line, this is hands-down the quickest way to open the file with the correct permissions. You do not need to navigate through folders or right-click anything.

Open your PowerShell or Command Prompt and paste this single command:

Start-Process -FilePath notepad.exe -Verb runas -ArgumentList "$env:SystemRoot\system32\drivers\etc\hosts"

This command tells Windows to start Notepad, elevate it to Administrator privileges (runas), and immediately open the hosts file. You will see the User Account Control (UAC) prompt, click Yes, and you are ready to edit.

Method 2: Using VS Code (For Developers)

As a developer, you probably already have VS Code open. The great thing about VS Code is that it handles permissions intelligently. You do not strictly need to launch it as an administrator from the start.

  1. Open VS Code or your terminal.
  2. Run the command: code "C:\Windows\System32\drivers\etc\hosts"
  3. Make your changes.
  4. When you try to save (Ctrl + S), VS Code will notice you lack permissions and show a popup button saying Retry as Admin.
  5. Click that button, accept the UAC prompt, and your file is saved.

This method is much smoother than the old Notepad way because it keeps you in your primary workflow.

Method 3: The Classic Notepad Way (Run as Administrator)

If you prefer sticking to the basics or are on a machine without dev tools, the traditional method still works perfectly. The trick is opening the editor before opening the file.

  1. Press the Start button and type Notepad.
  2. Right-click on Notepad and select Run as administrator.
  3. In Notepad, go to File > Open.
  4. Navigate to C:\Windows\System32\drivers\etc.
  5. Crucial Step: Change the file type dropdown from Text Documents (\.txt) to All Files (.*). If you skip this, the folder will look empty.
  6. Select the hosts file and click Open.

Method 4: Using Microsoft PowerToys (The Modern GUI)

If you modify your hosts file frequently, the Hosts File Editor utility in Microsoft PowerToys is a game-changer. It gives you a clean UI where you can toggle entries on and off with checkboxes instead of dealing with hashtags and IP strings.

You just open PowerToys, head to the Hosts File Editor tab, and click Launch Hosts File Editor. It automatically handles the admin permissions for you.

Troubleshooting: Access Denied & Read-Only Errors

Sometimes even with admin rights, Windows refuses to save the file. Here is how to fix it.

Check File Attributes

The file might be set to read-only.

  1. Navigate to C:\Windows\System32\drivers\etc.
  2. Right-click the hosts file and choose Properties.
  3. Ensure the Read-only attribute is unchecked.

Antivirus Interference

Some security software locks this file to prevent malware from redirecting your traffic. If you cannot save, temporarily pause your antivirus. If you are stuck in a loop of errors, you might want to boot into Safe Mode to rule out third-party interference. You can read our guide on How to Start Windows in Safe Mode to do this safely.

Why Edit the Hosts File?

You usually touch this file for two main reasons:

  1. Local Development: You want dev.example.com to point to your local machine (127.0.0.1) instead of the live server. This is essential for testing software deployment environments before pushing code to production. You can learn more about managing these environments in our software deployment environments guide.
  2. Troubleshooting DNS: Sometimes you need to force a domain to resolve to a specific IP to verify if a server is responding, bypassing public DNS records. This is a common step when diagnosing why a site is unreachable or when you see a DNS Probe Finished NXDOMAIN error. For a deeper dive into fixing that specific error, check out our guide on DNS Probe Finished NXDOMAIN.