Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Disconnect a VPN Connection in Windows Using PowerShell

In today's interconnected world, Virtual Private Networks (VPNs) are essential for secure communication over the internet. However, there are times when you need to disconnect from a VPN, whether to switch networks, troubleshoot issues, or simply end your session. In the Windows environment, PowerShell offers a straightforward way to manage VPN connections, including disconnecting from them. This article will guide you through the process of disconnecting a VPN connection using PowerShell, providing practical examples to illustrate each step.

Examples:

  1. Disconnecting a VPN Connection Using PowerShell:

    To disconnect a VPN connection, you can use the Disconnect-VpnConnection cmdlet in PowerShell. This cmdlet is part of the VpnClient module, which is included in Windows 10 and later versions.

    Step-by-Step Guide:

    a. Open PowerShell:

    • Press Win + X and select Windows PowerShell (Admin) to open PowerShell with administrative privileges.

    b. List Available VPN Connections:

    • Before disconnecting, you may want to list all available VPN connections to ensure you target the correct one. Use the following command:
      Get-VpnConnection
    • This command will display a list of all VPN connections configured on your system.

    c. Disconnect the VPN Connection:

    • Use the Disconnect-VpnConnection cmdlet to disconnect a specific VPN connection. Replace "YourVPNConnectionName" with the name of your VPN connection:
      Disconnect-VpnConnection -Name "YourVPNConnectionName" -Force
    • The -Force parameter ensures the connection is terminated without prompting for confirmation.
  2. Automating VPN Disconnection with a Script:

    If you frequently need to disconnect from a VPN, you can create a PowerShell script to automate the process.

    Example Script:

    # Define the VPN connection name
    $vpnName = "YourVPNConnectionName"
    
    # Check if the VPN connection is active
    $vpnConnection = Get-VpnConnection -Name $vpnName
    
    if ($vpnConnection.ConnectionStatus -eq "Connected") {
       # Disconnect the VPN connection
       Disconnect-VpnConnection -Name $vpnName -Force
       Write-Output "VPN connection '$vpnName' has been disconnected."
    } else {
       Write-Output "VPN connection '$vpnName' is not currently connected."
    }
    • Save the script with a .ps1 extension, for example, DisconnectVPN.ps1.
    • Run the script in PowerShell by navigating to the script's directory and executing:
      .\DisconnectVPN.ps1

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.