Powershell to manipulate host file

后端 未结 9 1726
我在风中等你
我在风中等你 2021-01-30 02:32

I am looking at to see if I can create powershell script to update the contents in the host file.

Anybody know if there are any examples that manipulate the host file u

9条回答
  •  失恋的感觉
    2021-01-30 02:55

    For me the biggest pain in dealing with the hosts file is remembering where it is. I set a variable that points to my hosts file in my PowerShell profile, which makes it easy to edit in a text editor.

    In PowerShell, type the following to open your profile:

    C:\> Notepad $profile
    

    Add this:

    $hosts = "$env:windir\System32\drivers\etc\hosts"
    

    Save the file, then close and re-open PowerShell, running as administrator. You can't edit the hosts file without elevated permissions.

    Now you can edit your hosts file the same way you'd edit your profile:

    C:\> Notepad $hosts
    

提交回复
热议问题