Skip to content

Instantly share code, notes, and snippets.

@k1ng440
Forked from danvy/WSL2-Net-Fix.ps1
Last active September 25, 2022 21:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k1ng440/b2b307adfb61e3f21b905745564beed0 to your computer and use it in GitHub Desktop.
Save k1ng440/b2b307adfb61e3f21b905745564beed0 to your computer and use it in GitHub Desktop.
Reset your WSL network connection trying to fix WSL2 media disconnected error
# Check these threads before proceeding:
# https://github.com/microsoft/WSL/discussions/5857
# https://github.com/microsoft/WSL/issues/5821
if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
$CmdLine = "-File `"" + $MyInvocation.MyCommand.Path + "`" " + $MyInvocation.UnboundArguments
Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList $CmdLine
Exit
}
Write-Host "Restarting WSL2"
# Restart the Host Network Service
Restart-Service -Force -Name hns
# Restart the Windows Subsystem for Linux Manager
Restart-Service LxssManager
# Restart the WSL Network adapter
Restart-NetAdapter -Name "vEthernet (WSL)"
wsl --shutdown
wsl ping google.com -c 4
wsl curl -v google.com
Function pause ($message) {
Write-Host "$message" -ForegroundColor Yellow
$x = $host.ui.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
pause "Press any key to exit"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment