Edit the Windows hosts file from the command line

Open the hosts file in Notepad as administrator, or append an entry with Add-Content.

WindowsFilesDNSadminhosts-fileMedium risk
Run `Start-Process notepad "$env:SystemRoot\System32\drivers\etc\hosts" -Verb RunAs` to edit the hosts file with admin rights.

01Command

Command
$ Start-Process notepad "$env:SystemRoot\System32\drivers\etc\hosts" -Verb RunAs

02What it does

The hosts file maps domain names to IP addresses before DNS is consulted — handy for testing a new server or blocking a site locally. Because it is a protected system file, it can only be edited by an administrator; the Start-Process -Verb RunAs command opens Notepad elevated after a UAC prompt. To add an entry without opening an editor, run an elevated `Add-Content -Path C:\Windows\System32\drivers\etc\hosts -Value "93.184.216.34 example.com"`.

03Risk

RiskMedium risk
RequirementsAdministrator elevation required (the command triggers the UAC prompt).
!Warning: Wrong entries can make websites unreachable. Add one line per mapping in the format `IP domain`, then run `ipconfig /flushdns` to apply.

04Undo

Undo
$ Remove the added lines from the hosts file to restore default name resolution.

05Sources

  • Reg - Read and edit the registry
    SS64 · technical · 2026-09-17
    Evidence: reg query syntax for reading registry values such as ProxyEnable under HKCU Internet Settings.
  • Get-NetConnectionProfile
    Microsoft Learn · vendor-doc · 2026-09-17
    Evidence: Returns the network category (Public/Private/DomainAuthenticated) of each active connection.

Last verified: 2026-09-18 · Category: Network

06Related solutions