Skip to content

Instantly share code, notes, and snippets.

@tombennett
Forked from lantrix/hosts.ps1
Created April 27, 2017 19:40
Show Gist options
  • Select an option

  • Save tombennett/6be44163c98cce65e94dbcfd236666cf to your computer and use it in GitHub Desktop.

Select an option

Save tombennett/6be44163c98cce65e94dbcfd236666cf to your computer and use it in GitHub Desktop.
Powershell script for adding/removing/viewing entries to the hosts file.
#
# Powershell Functions for adding/removing/showing entries to the hosts file.
#
# Known limitations:
# - does not handle entries with comments afterwards ("<ip> <host> # comment")
#
Function Add-Host {
<#
.SYNOPSIS
Powershell function for adding entries to the hosts file.
.PARAMETER HostsFile
The hosts file to modify
.PARAMETER IP
The IP address for the hosts entry
.PARAMETER Hostname
The hostname for the entry
.EXAMPLE
Add-Host -HostsFile "C:\Windows\System32\drivers\etc\hosts" -IP "127.0.0.1" -Hostname "myserverhost"
#>
[cmdletbinding()]
param(
[Parameter(Mandatory = $true)][string]$HostsFile,
[Parameter(Mandatory = $true)][string]$IP,
[Parameter(Mandatory = $true)][string]$Hostname
)
Remove-Host -HostsFile $HostsFile -Hostname $Hostname
$IP + "`t`t" + $Hostname | Out-File -encoding ASCII -append $HostsFile
}
Function Remove-Host {
<#
.SYNOPSIS
Powershell function for removing entries from a hosts file.
.DESCRIPTION
Known limitations: does not handle entries with comments afterwards ("<ip> <host> # comment")
.PARAMETER HostsFile
The hosts file to modify
.PARAMETER Hostname
The hostname for the entry
.EXAMPLE
Remove-Host -HostsFile "C:\Windows\System32\drivers\etc\hosts" -Hostname "myserverhost"
#>
[cmdletbinding()]
param(
[Parameter(Mandatory = $true)][string]$HostsFile,
[Parameter(Mandatory = $true)][string]$Hostname
)
$c = Get-Content $HostsFile
$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 $HostsFile
foreach ($line in $newLines) {
$line | Out-File -encoding ASCII -append $HostsFile
}
}
Function Show-Hosts {
<#
.SYNOPSIS
Powershell function for showing entries in a hosts file.
.DESCRIPTION
Known limitations: does not handle entries with comments afterwards ("<ip> <host> # comment")
.PARAMETER HostsFile
The hosts file to access
.EXAMPLE
Show-Hosts -HostsFile "C:\Windows\System32\drivers\etc\hosts"
#>
[cmdletbinding()]
param(
[Parameter(Mandatory = $true)][string]$HostsFile
)
$c = Get-Content $HostsFile
foreach ($line in $c) {
$bits = [regex]::Split($line, '\t+')
if ($bits.count -eq 2) {
Write-Host $bits[0] `t`t $bits[1]
}
}
}
#
# Example Usage
#
$file = "$env:SystemRoot"+"\System32\drivers\etc\hosts"
try {
if ($args[0] -eq "add") {
if ($args.count -lt 3) {
throw "Not enough arguments for add."
} else {
Add-Host -HostsFile $file -IP $args[1] -Hostname $args[2]
}
} elseif ($args[0] -eq "remove") {
if ($args.count -lt 2) {
throw "Not enough arguments for remove."
} else {
Remove-Host -HostsFile $file -Hostname $args[1]
}
} elseif ($args[0] -eq "show") {
Show-Hosts -HostsFile $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"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment