#PSTip Editing your hosts file

Note: This tip requires PowerShell 2.0 or above.

Occasionally¬†you need to make changes to the hosts file, either locally or remotely on another machine. There are times when you open the file, but when you try to save it you are presented with the “save as” dialog, which suggests that the text editor you are working with was not opened as an administrator. As a result, you are forced to close the file, reopen it with elevated editor, and make your changes again.

For that purpose I keep the Edit-HostsFile function in my profile. It takes care of elevation for me (assuming you have the appropriate permissions), making changes to hosts files has never been so easy.

function Edit-HostsFile

	Start-Process notepad.exe -ArgumentList \\$ComputerName\admin$\System32\drivers\etc\hosts -Verb RunAs

About the author: Shay Levy

Shay Levy is a Co-founder and editor of the PowerShell Magazine. He is a multiple-year recipient of the Microsoft MVP award, and a Microsoft Certified Trainer (MCT). Shay often covers PowerShell related topics on his blog and you can also follow him on Twitter at @ShayLevy

Related Posts