Skip to content

Instantly share code, notes, and snippets.

@fredyfx
Last active March 22, 2026 11:38
Show Gist options
  • Select an option

  • Save fredyfx/4ead278f00e0b40b3a9eec4e2c68a9eb to your computer and use it in GitHub Desktop.

Select an option

Save fredyfx/4ead278f00e0b40b3a9eec4e2c68a9eb to your computer and use it in GitHub Desktop.
Basically I got tired of restarting manually the Network in order to have my Linux Distros running properly under Windows 11 as WSL. I think I'm not the only one with the same situation, so, here is the script, enjoy!
# ==============================================================================
# Restart-WSL-Network.ps1
# Restarts the Hyper-V virtual network adapter used by WSL2.
# Fixes: DNS resolution failures, "Temporary failure in name resolution",
# and general network connectivity issues inside WSL2.
#
# Usage:
# Right-click > "Run with PowerShell" (as Administrator)
# -- or --
# PowerShell (Admin): .\Restart-WSL-Network.ps1
#
# License: MIT
# ==============================================================================
#Requires -RunAsAdministrator
$ErrorActionPreference = "Stop"
# ---------------------------------------------------------------------------- #
# Helpers
# ---------------------------------------------------------------------------- #
function Write-Header {
Write-Host ""
Write-Host " +--------------------------------------+" -ForegroundColor Cyan
Write-Host " | WSL2 Network Reset Utility |" -ForegroundColor Cyan
Write-Host " +--------------------------------------+" -ForegroundColor Cyan
Write-Host ""
}
function Write-Step([string]$msg) {
Write-Host " >> " -NoNewline -ForegroundColor Yellow
Write-Host $msg
}
function Write-Success([string]$msg) {
Write-Host " OK " -NoNewline -ForegroundColor Green
Write-Host $msg
}
function Write-Failure([string]$msg) {
Write-Host " !! " -NoNewline -ForegroundColor Red
Write-Host $msg
}
function Write-Info([string]$msg) {
Write-Host " -- " -NoNewline -ForegroundColor Gray
Write-Host $msg -ForegroundColor Gray
}
# ---------------------------------------------------------------------------- #
# Main
# ---------------------------------------------------------------------------- #
Write-Header
# 1. Find the WSL Hyper-V adapter
Write-Step "Searching for WSL Hyper-V network adapter..."
$adapter = Get-NetAdapter | Where-Object { $_.Name -like "*WSL*" } | Select-Object -First 1
if (-not $adapter) {
Write-Failure "No WSL adapter found. Is WSL2 installed?"
Write-Info "Run 'wsl --install' and try again."
exit 1
}
Write-Success "Found adapter: '$($adapter.Name)' -- Status: $($adapter.Status)"
Write-Host ""
# 2. Shut down all WSL instances
Write-Step "Shutting down all WSL2 instances..."
try {
wsl --shutdown
Write-Success "WSL2 shut down."
} catch {
Write-Failure "Could not shut down WSL2: $_"
exit 1
}
Start-Sleep -Seconds 2
# 3. Disable the adapter
Write-Step "Disabling adapter '$($adapter.Name)'..."
try {
Disable-NetAdapter -Name $adapter.Name -Confirm:$false
Start-Sleep -Seconds 3
Write-Success "Adapter disabled."
} catch {
Write-Failure "Failed to disable adapter: $_"
exit 1
}
# 4. Re-enable the adapter
Write-Step "Re-enabling adapter '$($adapter.Name)'..."
try {
Enable-NetAdapter -Name $adapter.Name
Start-Sleep -Seconds 3
Write-Success "Adapter re-enabled."
} catch {
Write-Failure "Failed to re-enable adapter: $_"
exit 1
}
# 5. Verify adapter is back up
$updatedAdapter = Get-NetAdapter -Name $adapter.Name
if ($updatedAdapter.Status -eq "Up") {
Write-Success "Adapter is Up and running."
} else {
Write-Failure "Adapter status is '$($updatedAdapter.Status)'. You may need to reboot."
}
# 6. Done
Write-Host ""
Write-Host " ============================================" -ForegroundColor Cyan
Write-Host " Done! Now open WSL and test:" -ForegroundColor White
Write-Host " ping -c 3 8.8.8.8" -ForegroundColor Yellow
Write-Host " ping -c 3 google.com" -ForegroundColor Yellow
Write-Host ""
Write-Info "If DNS still fails inside WSL, run:"
Write-Info " sudo chattr -i /etc/resolv.conf 2>/dev/null"
Write-Info " echo -e 'nameserver 8.8.8.8\nnameserver 8.8.4.4' | sudo tee /etc/resolv.conf"
Write-Info " sudo chattr +i /etc/resolv.conf"
Write-Host " ============================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "Powered by @fredyfx - https://github.com/fredyfx" -ForegroundColor Green
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment