Skip to content

Instantly share code, notes, and snippets.

@KristofferRisa
Last active July 26, 2021 22:03
Show Gist options
  • Save KristofferRisa/e2316514b3bf8b92a461 to your computer and use it in GitHub Desktop.
Save KristofferRisa/e2316514b3bf8b92a461 to your computer and use it in GitHub Desktop.
Hosts.ps1 is a PowerShell script for searching, adding, removing, and viewing the windows hosts file.

HOSTS

Hosts.ps1 is a PowerShell script for searching, adding, removing, and viewing the windows hosts file from the terminal.

Exmaple


Usage: hosts add  
       hosts remove 
       hosts show
       hosts search 
       hosts open

Install

  1. Open the "terminal" in administrator mode.
  2. Copy and the paste the code under:

Invoke-Command -ScriptBlock ([Scriptblock]::Create((Invoke-WebRequest -Uri 'https://gist.githubusercontent.com/KristofferRisa/e2316514b3bf8b92a461/raw/14f425eda1b906c2ef16289d639218304bb695ce/install-host.ps1' -UseBasicParsing).Content))

  1. Done!

About

The script installs the "hosts.ps1" in the c:\tools\ folder (creates the tools folder if it doesn't exist). Then it adds the "c:\tools" folder into your PATH Environment variables so that you now can type "hosts" anywhere in your terminal

LICENSE

MIT

$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 Find-Hostname([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]
}
}
}
function search-hosts([string]$filename, [string]$hostname){
$c = Get-Content $filename
$count = 0;
$hostname = '*'+$hostname+'*'
Write-Host "Searching for "$hostname
foreach ($line in $c) {
$bits = [regex]::Split($line, "\t+")
if ($bits.count -eq 2) {
if($bits[0] -like $hostname -or $bits[1] -like $hostname) {
$count++;
Write-Host $bits[0] `t`t $bits[1]
}
}
}
if($count -eq 0){
Write-Host "No results"
}
}
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") {
gc $file
} elseif ($args[0] -eq "open"){
try {
Start-Process 'C:\Program Files (x86)\Notepad++\notepad++.exe' $file
}
catch {
Start-Process 'notepad.exe' $file
}
} elseif($args[0] -eq "search"){
Write-Host 'test'
Find-Hostname $file $args[1]
}
else {
throw "Invalid operation '" + $args[0] + "' - must be one of 'add', 'remove', 'show', 'search + [hosts]' or 'open'."
}
} catch {
Write-Host $error[0]
Write-Host "`nUsage: hosts add <ip> <hostname>`n hosts remove <hostname>`n hosts show`n hosts search <hostname>`n hosts open"
}
# Creates local folder (C:\Tools)
if(-not (Test-Path 'C:\Tools'))
{
mkdir c:\Tools
}
if(Test-Path 'C:\Tools\hosts.ps1')
{
del C:\Tools\hosts.ps1
}
[System.Environment]::SetEnvironmentVariable("PATH", $Env:Path + ";C:\Tools", "User")
# Downloads the script
(new-object net.webclient).DownloadFile('https://gist.githubusercontent.com/KristofferRisa/e2316514b3bf8b92a461/raw/3cbe168a13ee7ac8ba93205e183e9ad5f732a257/hosts.ps1', 'c:\Tools\hosts.ps1')
@sergeevabc
Copy link

Broken. To start with, there is no such thing as print-hosts $file, replace it with gc $file.

@KristofferRisa
Copy link
Author

Thanks for your feedback @sergeevabc! I have updated the install and the hosts script 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment