Powershell Regex find name VPN connection

Hi ppl ) 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!

AlexPebody,
Welcome to the forum. :wave:t3:

I cannot test but how about using

Get-VpnConnection | 
    Where-Object -Property Name -Match -Value 'MyVPN'

Actually you could even filter for only connected VPN connections when you combine both conditions like this:

Get-VpnConnection | 
    Where-Object {
        $_.Name -Match 'MyVPN' -and
        $_.ConnectionStatus -eq 'Connected'
    }

Thx. I found this way:
$vpn = Get-VpnConnection -AllUserConnection | Where-Object { $_.Name -like ‘vash’ }
if($vpn.ConnectionStatus -eq “Connected”){
rasdial $vpnName /DISCONNECT;
}

But new important task, how I can set: Set-VPNconnection -name ??? -SplitTunneling $false
I’ve tried more than 100 ways and nothing… I need set Default Gateway, if I set it directly like: Set-VPNconnection -name “VPN Vashinsider” -SplitTunneling $true all is ok, but what I can do if I know part name only, like “vash” it’s important for me.

$vpn = Get-VpnConnection -AllUserConnection | Where-Object { $_.Name -like ‘vash’ } | foreach { Set-VPNconnection -name $vpn[0].Name -SplitTunneling $false } and other NOT WORKING ((((

First of all … please format your code as code here in the forum. Just like you already did in your initial question.

Have you actually tried my suggestion?

You can use the same approach like for the other task?! :man_shrugging:t3: You query the VPN connection you’re after with Get-VPNConnection and pipe the result to Set-VPNConnection.

Get-VpnConnection -AllUserConnection | 
    Where-Object { $_.Name -match 'vash' } |
        Set-VpnConnection -SplitTunneling $true

If it’s a partial match with the like operator you need '*'s around vash such as $_.Name -like '*vash*'

So thx, but Gateway not changed with script: Get-VpnConnection -AllUserConnection | Where-Object { $_.Name -match ‘vash’ } | Set-VpnConnection -SplitTunneling $false

But if I set directly: Set-VPNconnection -name “VPN Vashinsider” -SplitTunneling $true all is ok, why it could be?

And this not working too: Get-VpnConnection -AllUserConnection | Where-Object { $.Name -like ‘vash’ } | foreach { Set-VPNconnection -name $.Name -SplitTunneling $false }

Gateway NOT changes. Why?

Yes I am using '*vash*'

Get-VpnConnection -AllUserConnection | Where-Object { $_.Name -match '*vash*' } | Set-VpnConnection -SplitTunneling $false

Get-VpnConnection -AllUserConnection | Where-Object { $_.Name -like '*vash*' } | foreach { Set-VPNconnection -name $_.Name -SplitTunneling $false }

Not working, gateway NOT changed…

OMG! It WORKS!

Get-VpnConnection | Where-Object { $_.Name -like '*vash*' } | foreach { Set-VPNconnection -name $_.Name -SplitTunneling $false }

SO THX!