How to read the windows host file using PowerShell?


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,

Example

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

Output

# 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

To get the host file from the remote server,

Get-Content \Computer1\c$\windows\system32\drivers\etc\hosts

Updated on: 18-Mar-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements