Edit hosts file from within PowerShell



The hosts file is a simple text file that is used by your computer’s operating system to map domain names to IP addresses. It is often used to map a domain name to a local IP address for development or testing purposes, or for blocking access to certain websites. Editing the hosts file from within PowerShell can be a useful and powerful tool for managing your machine’s network configuration.

To edit the hosts file from within PowerShell, you first need to open an administrative PowerShell window. To do this, launch PowerShell and then type “Run as Administrator” into the command line. This will open a separate PowerShell window with administrative privileges.

Once you have the administrative PowerShell window open, you can use the built-in cmdlet “Set-Content” to edit the hosts file. To do this, type “Set-Content -Path C:\\Windows\\System32\\drivers\\etc\\hosts” into the command line. This will open the hosts file in the PowerShell window. You can then use the “Add-Content” cmdlet to add new lines to the hosts file. For example, to block access to a website, you could use the following command: “Add-Content -Path C:\\Windows\\System32\\drivers\\etc\\hosts -Value ‘127.0.0.1 www.example.com’”. This will add a line to the hosts file that will map the domain “www.example.com” to the local address “127.0.0.1”, effectively blocking access to the website.

In addition to adding new lines to the hosts file, you can also use the “Remove-Content” cmdlet to remove existing lines. To do this, you can use the “-Exclude” parameter. For example, if you wanted to remove the line you added earlier to block access to “www.example.com”, you could use the following command: “Remove-Content -Path C:\\Windows\\System32\\drivers\\etc\\hosts -Exclude ‘127.0.0.1 www.example.com’”. This would remove the line from the hosts file, allowing access to the website again.

Finally, you can use the “Get-Content” cmdlet to view the contents of the hosts file. To do this, type “Get-Content -Path C:\\Windows\\System32\\drivers\\etc\\hosts” into the command line. This will display all of the lines in the hosts file, allowing you to easily view any changes you have made.

Editing the hosts file from within PowerShell can be a powerful and convenient way to easily manage your machine’s network configuration. By using the “Set-Content”, “Add-Content”, “Remove-Content”, and “Get-Content” cmdlets, you can easily add, remove, and view the contents of the hosts file from within PowerShell. This can be a great tool for IT admins who need to quickly manage their machine’s network configuration.

Inquire Now

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Find your next full or part-time role here

ScaleDesk can either help you find your next full time gig or a part time side gig where you can create extra income!

Onboard with us once
Skip HR screening and go to the final interview with with only your resume and a video interview you never have to redo
Get paid electronically every month for the hours you work
We will be your reference even if you work for us once

IT Teams: Use ScaleDesk to augment your team

Schedule Demo