How to read the windows host file using PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

Windows Host file that maps the hostname and the IP address is the plain text file. So reading the host file is simpler. We just need to use the Get-Content command to retrieve the file content.

Generally, the host file is stored at the location c:\windows\System32\drivers\etc\.  If the OS is installed at that location.

Or you can use $env:Windir to get the windows directory.

To get the file content,


Get-Content $env:windir\system32\drivers\etc\hosts


# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
# For example:
#          # source server
#              # x client host

# localhost name resolution is handled within DNS itself.
#       localhost
#       ::1             localhost

To get the host file from the remote server,

Get-Content \\Computer1\c$\windows\system32\drivers\etc\hosts
Published on 18-Mar-2021 07:43:49