Powershell Regex find name VPN connection

Alexey 1 Reputation point
Hi all ) please need a help, I have a scipt to disconnect VPN (powershell):

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

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

Please tell me, how I can find and set var $vpnName if I know just part of VPN name, like "vash", full VPN name maybe VPNvashinsider or VPN vashinsider or vpnvashinsider.com

I need regex how I can set var $vpnName taked from part of word "vash"? Thanks a lot!
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
747 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 39,261 Reputation points

    Something like this might work for you:

    $vpnName = "MyVPN";
    [array]$vpns = Get-VpnConnection |
                Where-Object {$_.Name -Like "*$vpnName" -AND $_.ConnectionStatus -eq "Connected"}
    if ($vpns.count -eq 1){
        rasdial $vpnName /DISCONNECT
        Write-Host "Multiple, or no, ($($vpns.count)) VPN connections match '$vpnName'"
    0 comments No comments