Windows VPN Keep Alive

While Windows 10 makes it very easy to create a VPN and connect to it, it doesn’t seem particularly stable, and won’t auto reconnect if it does drop out. This PowerShell script will keep your VPN alive.

Simply change “ENTER VPN NAME HERE” in the script below to the name of your VPN, and run it in PowerShell. It’ll pop up a credential window prompting you to enter your username and password for the VPN, then refresh every 10 seconds and re-connect if the VPN has disconnected.

If you don’t care about someone potentially seeing your password, replace the $Credential.UserName and $Credential.GetNetworkCredential().password with your username and password, and remove the Get-Credential line. Just be aware your login details will be in plain text in the PowerShell window for anyone looking over your shoulder to see!

When you want to disconnect the VPN, simply CTRL+C and it will end the loop and disconnect!

Clear-Host
$Credential = Get-Credential -Message "Enter VPN login details:"
$VPNName = "ENTER VPN NAME HERE"
try
    {
        while($true)
            {
                $VPNStatus = rasdial.exe
                If($VPNStatus -NotContains $VPNName)
                    {
                        "VPN disconnected, reconnecting at $(Get-Date)."
                        rasdial.exe $VPNName $Credential.UserName $Credential.GetNetworkCredential().password
                    }
                Start-Sleep -s 10
                
            }
    }
finally
    {
        write-host "Disconnecting $VPNName"
        rasdial.exe $VPNName /DISCONNECT
    }

Leave a Reply

Your email address will not be published.