-
-
Save markembling/173887 to your computer and use it in GitHub Desktop.
# | |
# Powershell script for adding/removing/showing entries to the hosts file. | |
# | |
# Known limitations: | |
# - does not handle entries with comments afterwards ("<ip> <host> # comment") | |
# | |
$file = "C:\Windows\System32\drivers\etc\hosts" | |
function add-host([string]$filename, [string]$ip, [string]$hostname) { | |
remove-host $filename $hostname | |
$ip + "`t`t" + $hostname | Out-File -encoding ASCII -append $filename | |
} | |
function remove-host([string]$filename, [string]$hostname) { | |
$c = Get-Content $filename | |
$newLines = @() | |
foreach ($line in $c) { | |
$bits = [regex]::Split($line, "\t+") | |
if ($bits.count -eq 2) { | |
if ($bits[1] -ne $hostname) { | |
$newLines += $line | |
} | |
} else { | |
$newLines += $line | |
} | |
} | |
# Write file | |
Clear-Content $filename | |
foreach ($line in $newLines) { | |
$line | Out-File -encoding ASCII -append $filename | |
} | |
} | |
function print-hosts([string]$filename) { | |
$c = Get-Content $filename | |
foreach ($line in $c) { | |
$bits = [regex]::Split($line, "\t+") | |
if ($bits.count -eq 2) { | |
Write-Host $bits[0] `t`t $bits[1] | |
} | |
} | |
} | |
try { | |
if ($args[0] -eq "add") { | |
if ($args.count -lt 3) { | |
throw "Not enough arguments for add." | |
} else { | |
add-host $file $args[1] $args[2] | |
} | |
} elseif ($args[0] -eq "remove") { | |
if ($args.count -lt 2) { | |
throw "Not enough arguments for remove." | |
} else { | |
remove-host $file $args[1] | |
} | |
} elseif ($args[0] -eq "show") { | |
print-hosts $file | |
} else { | |
throw "Invalid operation '" + $args[0] + "' - must be one of 'add', 'remove', 'show'." | |
} | |
} catch { | |
Write-Host $error[0] | |
Write-Host "`nUsage: hosts add <ip> <hostname>`n hosts remove <hostname>`n hosts show" | |
} |
very useful thanks. Using it to auto create IIS sites, app pools, dirs and host files
how to execute proper function?
gonna be using this on Domainiac as part of the app
@cicciobart Usage of the script could be as follows:
Add a new host to the file:
./hosts.ps1 add 127.0.0.1 myhostname.example.com
Remove a host from the file:
./hosts.ps1 remove myhostname.example.com
Show the contents of the hosts file:
./hosts.ps1 show
I'd add a clearer comment on the removal of the 'n in the last catch section to make it clearer that by default, doing an add or remove action overwrites all entries in the files. Great script!
I'm not sure what you mean @tylercorelitz.
The entire contents of the file get replaced but that's inevitable for the remove operation. It needs to essentially read the current contents of the file and then replace it with what was there before but without the line which is to be removed. So yes, the entire file is rewritten but the new contents should be whatever was there before except for the removed entry.
When you do an add, it will attempt a removal of the hostname you're adding to avoid the possibility of it having been added twice.
Thanks, in my usage doing an add or remove was removing all data. if doing an add the hosts file would be blank except for the added item, if doing a remove all items will be removed. As the first comment states, editing this section of the script to remove the n after hostname, resolved the issue.
} catch {
Write-Host $error[0]
Write-Host "nUsage: hosts add <ip> <hostname>
n hosts remove `n hosts show"
}
for removing a host, if you simply append a `n at the end of each line, then just write out the newLines as a text, it saves exponential work compared to simply writing over the files n times (# of lines)! I only noticed it got really slow since my host file has many, many lines from spybot entries, but though it'd speed it up for some other people too :)