Editing the .\etc\hosts file on Windows 10

9,019

You don't have to invoke PowerShell as elevated administrator, but it helps if you have to do multiple operations that require admin permission.

To edit the hosts file from a normal PowerShell session use:

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

You will be prompted for administrator credentials and then notepad.exe will be started elevated and you can edit the hosts file.

Note the -Verb runas in the command, it tells PowerShell to start the process with with a higher Integrity level.

Don't try to change the NTFS permissions on the hosts file to edit it as a normal user. This is a security risk and the permissions may be reset by the next Windows Update or a repair process.

Share:
9,019

Related videos on Youtube

hgiesel
Author by

hgiesel

Job hunting in Frankfurt area. In relationship with functional programming. It's serious.

Updated on September 18, 2022

Comments

  • hgiesel
    hgiesel almost 2 years

    I tried multiple ways to edit hosts file on Windows but failed.

    First the hosts file seemed to appear, when using PowerShell, but the whole \etc\ directory disappeared when using any other tool. But because I don't know how to open as admin in PowerShell (I guess it's not possible), I just created a new etc directory with notepad run as an admin.

    But now the information provided in hosts isn't used by any program (browser or putty). As a Unix nerd, this is very frustrating to me. How can I edit it on Windows 10? I also wouldn't mind some GUI thing hidden somewhere deep in the Settings app providing the same functionality.

    • djsmiley2kStaysInside
      djsmiley2kStaysInside over 7 years
      /etc/hosts is something that exists in linux, not windows 10 as far as I'm aware. Where are you trying to edit this file? Your question is currently un clear.
    • hgiesel
      hgiesel over 7 years
      It exists under C:\windows\system32\drivers\etc\hosts on every Windows machine.
    • LPChip
      LPChip over 7 years
      If you want to run something as administrator in PowerShell, then start PowerShell itself as administrator.
    • Ramhound
      Ramhound over 7 years
      You have to open your text editor as an Administrator that's the only way.
    • LPChip
      LPChip over 7 years
      @Ramhound its not the only way. It is possible to set the rights properly and then you don't need to. There's a plugin for Chrome which will request changing rights in order for it to work, and then you don't need administrative permissions anymore.
    • Ramhound
      Ramhound over 7 years
      @LPChip - I was not aware of that.
  • LPChip
    LPChip over 7 years
    No need to reboot. I found out that most of the time the changes are instant, and in the cases where it isn't, an ipconfig /flushdns is sufficient. Also, don't forget the browser's cache.
  • Ramhound
    Ramhound over 7 years
    If your using the hosts file to block content your blocking content in an inefficient way. You can use hosts to define, hosts on your network, when it's not applicable to add the hosts to your dns. If you define hundreds or thousands of hosts entries you will slow your browsing down
  • Frank Thomas
    Frank Thomas over 7 years
    I generally use the host file for redirecting traffic into infrastructure tunnels, and for mocking out test environments for servers where code is deployed that uses DNS names in conjunction with Active Directory (so we can't point to a mock dns server). For instance, right now I'm testing a bunch of web apps on a new server, that will replace an existing box when its ready, and host file rules are what makes the production links point to test resources while testing commences. As for reboots, I've set up hosts file on 3 boxes in the last few months, and all required rebooting for me.