Powershell to manipulate host file

后端 未结 9 1713
我在风中等你
我在风中等你 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:41

    I have written a code to delete entries from host. You can easily change the code to add entries to it from the code.

    $domainName = "www.abc.com"
    $rplaceStr = ""
    $rHost = "C:\Windows\System32\drivers\etc\hosts"
    $items = Get-Content $rHost | Select-String $domainName
    Write-host $items
    foreach( $item in $items)
    {
    (Get-Content $rHost) -replace $item, $rplaceStr| Set-Content $rHost
    }
    

    For more information see http://nisanthkv.blog.com/2012/06/13/remove-host-entries-using-powershell/

提交回复
热议问题