#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
{
	param($ComputerName=$env:COMPUTERNAME)
	Start-Process notepad.exe -ArgumentList \\$ComputerName\admin$\System32\drivers\etc\hosts -Verb RunAs
}
Share on: