Last active
January 29, 2024 13:15
-
-
Save subframe7536/19b91a51e353c7704b045037da5ba7c1 to your computer and use it in GitHub Desktop.
Github Hosts Update Script For Windows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
need admin permission