Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Disconnect VPN in Windows Via Powershell

I'm always connecting and disconnecting to my VPN and I have a powershell script that connects to the VPN profile and it's brilliant. I dont have to type anything just run it.

However, I always needed to disconnected via the convoluted GUI.

The awesome script I use to connect goes something like:

$vpnName = "MyVPN";
    $vpn = Get-VpnConnection -Name $vpnName;
    if($vpn.ConnectionStatus -eq "Disconnected"){
    rasdial $vpnName;
    }

Is there a way to do the opposite?

I cannot seem to find any way of disconnecting via powershell. I cannot use third party software and I can't seem to find anything in PS get-help Vpnconnection that disconnects.

like image 477
Daniel Avatar asked Jan 03 '23 16:01

Daniel


1 Answers

rasdial has a parameter /DISCONNECT that should do what you want.

You could rewrite your script to do the disconnect like this:

$vpnName = "MyVPN";
$vpn = Get-VpnConnection -Name $vpnName;

if($vpn.ConnectionStatus -eq "Connected"){
  rasdial $vpnName /DISCONNECT;
}

I haven't tested this, but it should work.

like image 144
kim Avatar answered Jan 05 '23 06:01

kim