Skip to content

Instantly share code, notes, and snippets.

@subframe7536
Last active January 29, 2024 13:15
Show Gist options
  • Select an option

  • Save subframe7536/19b91a51e353c7704b045037da5ba7c1 to your computer and use it in GitHub Desktop.

Select an option

Save subframe7536/19b91a51e353c7704b045037da5ba7c1 to your computer and use it in GitHub Desktop.
Github Hosts Update Script For Windows
$hostsFilePath = "C:\Windows\System32\drivers\etc\hosts"
$tempFilePath = "C:\hosts.txt"
$url = "https://raw.hellogithub.com/hosts"
$startLine = "# GitHub520 Host Start"
$endLine = "# GitHub520 Host End"
# Check if the line exists in the hosts file
$lineExists = Select-String -Path $hostsFilePath -Pattern $startLine -Quiet
# If the line exists, remove the old segment
if ($lineExists) {
$content = Get-Content $hostsFilePath
$startLineIndex = $content.IndexOf($startLine)
$endLineIndex = $content.IndexOf($endLine)
if ($startLineIndex -ne -1 -and $endLineIndex -ne -1) {
if ($startLineIndex -ne 0) {
$dns = $content[0..($startLineIndex - 1)]
}
if ($endLineIndex + 1 -ne $content.Length) {
$dns = $dns + $content[($endLineIndex + 1)..($content.Length - 1)]
}
$dns | Out-File $hostsFilePath -Encoding UTF8
}
}
# Download the hosts file from the specified URL and append it to the local hosts file
Invoke-WebRequest -Uri $url -OutFile $tempFilePath
Add-Content -Path $hostsFilePath -Value (Get-Content -Path $tempFilePath)
# Remove the temporary file
Remove-Item -Path $tempFilePath
# Flush the DNS cache
cmd /c ipconfig /flushdns
@subframe7536
Copy link
Author

need admin permission

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