-
Notifications
You must be signed in to change notification settings - Fork 3
/
remove-fromHostFile.ps1
41 lines (32 loc) · 1.13 KB
/
remove-fromHostFile.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<#
This script is meant to rewrite the hostfile.
The hostfile can be pesky with it's encoding so it is important that we write the file in a certain way.
The script removes an row from the hostfile. It can be easily modified to add lines as well.
Syntax: ./remove-fromHostFile -ip "173.194.74.121" -site "blog.gferreira.me"
Additional info can be found here: http://blog.gferreira.me/2011/01/yank-out-that-entry.html
#>
param ($ip, $site)
if($ip -and $site)
{
#Get content and create a backup file of the host file
$hostFile = Get-Content "C:\Windows\System32\drivers\etc\hosts"
$hostFile | Out-File "C:\Windows\System32\drivers\etc\hosts-BAKUP"
#Parse through file and find correct line to remove
foreach ($line in $hostFile)
{
if ($line -eq "$ip $site")
{
Write-Output "Found the line! Excluding it from new host file"
}
else
{
$newHost += $line
}
}
#Export HostFile using correct encoding
$newHost | Out-File C:\Windows\System32\drivers\etc\hosts -Encoding:ascii -ErrorAction:SilentlyContinue
}
else
{
Write-Host "Both IP and site are required"
}