SkillPilotAI

PowerShell VpnClient module

The VpnClient module manages Windows VPN profiles from PowerShell: create and change profiles, add routes for split tunneling, tune IPsec and proxy settings, and set triggers that auto-connect the VPN. Split vs full tunnel is the key concept to understand.

228.1 What it does, where it runs

  • Built into Windows 10/11 and Windows Server; PowerShell 5.1 and 7.
  • Manages VPN profiles in the Connection Manager phone book: add, set, remove, routes, triggers.
  • The module has 19 cmdlets.
  • No VpnClient cmdlet was run on the office PC (Get-VpnConnection would reveal office VPN names and servers); outputs are from the Microsoft Learn docs and a WireGuard lab.
PowerShell
Get-Command -Module VpnClient |  Measure-ObjectGet-Help Add-VpnConnection
Output
$ Get-Command -Module VpnClient | Measure-ObjectCount    : 19$ Get-Help Add-VpnConnectionNAME    Add-VpnConnectionSYNTAX    Add-VpnConnection [-Name] <string> [-ServerAddress] <string> [[-SplitTunneling]] [[-RememberCredential]] [-PlugInApplicationID] <string> -CustomConfiguration <xml> [-Force] [-PassThru]     Add-VpnConnection [-Name] <string> [-ServerAddress] <string> [[-TunnelType] {Pptp | L2tp | Sstp | Ikev2 | Automatic}] [[-EncryptionLevel] {NoEncryption | Optional | Required | Maximum | Custom}] REMARKS    Get-Help cannot find the Help files for this cmdlet on this computer. It is displaying only partial help.

Two syntax sets: the first for a third-party plug-in profile, the second for the Windows inbox profile.

228.2 How it works: split vs full tunnel

Split tunnel: only office traffic in the tunnel
  1. 1.Laptop: Contoso VPN connected

  2. 2.Packet for the office file server enters the encrypted tunnel

  3. 3.Tunnel crosses the Internet to the VPN gateway vpn.contoso.com

  4. 4.Gateway decrypts and forwards to file server 172.31.99.10

  5. 5.YouTube traffic does not enter the tunnel

  6. 6.It goes directly over the Internet to the website

Full tunnel: all traffic through the gateway
  1. 1.SplitTunneling: False

  2. 2.YouTube packet also enters the tunnel

  3. 3.Travels over the Internet to the VPN gateway

  4. 4.Gateway forwards it on to the website

  5. 5.Office policy applies to all traffic

  6. 6.Cost: more gateway load and more latency

PointSplit tunnelFull tunnel
What enters the tunnelOnly routed prefixes (Add-VpnConnectionRoute)Everything
Internet browsingDirect, not protected by the VPNThrough the gateway
Gateway load / latencyLowHigher
VPN proxy (Set-VpnConnectionProxy)Not used; general proxy settings applyUsed
Setting-SplitTunneling ($true)-SplitTunneling $False (default on Add)

228.3 Add-VpnConnection (24)

PowerShell
Add-VpnConnection -Name "Test1" `  -ServerAddress "10.1.1.1" -PassThru
Output
Name                  : Test1ServerAddress         : 10.1.1.1AllUserConnection     : FalseTunnelType            : AutomaticAuthenticationMethod  : {MsChapv2}EncryptionLevel       : RequiredL2tpIPsecAuth         : CertificateUseWinlogonCredential : FalseConnectionStatus      : DisconnectedRememberCredential    : FalseSplitTunneling        : False
  • Defaults: TunnelType Automatic, MsChapv2, encryption Required, SplitTunneling False; ConnectionStatus Disconnected (adding is not connecting).
  • -Name and -ServerAddress are required; -TunnelType Pptp|L2tp|Sstp|Ikev2|Automatic (Ikev2/Sstp modern, Pptp old and weak).
  • -AuthenticationMethod Pap|Chap|MSChapv2|Eap|MachineCertificate; -EncryptionLevel NoEncryption|Optional|Required|Maximum|Custom.
  • -AllUserConnection = global phone book; -RememberCredential caches credentials; -L2tpPsk pre-shared key; -ServerList multiple servers; -EapConfigXmlStream from New-EapConfiguration.

228.4 Routes, Get, Remove and server lists

PowerShell
Add-VpnConnectionRoute -ConnectionName `  "Contoso" -DestinationPrefix `  "176.16.0.0/16" -PassThruRemove-VpnConnectionRoute -ConnectionName `  "Contoso" -DestinationPrefix `  "176.16.0.0/16" -PassThruGet-VpnConnection -Name "Test3" `  -AllUserConnectionNew-VpnServerAddress -ServerAddress `  "RAS.Washington.Contoso.com" `  -FriendlyName "Washington"Remove-VpnConnection -Name "Test3",  "Test4",`  "Test5" -AllUserConnection -Force -PassThru
Output
DestinationPrefix : 176.16.0.0/16InterfaceAlias    : ContosoAddressFamily     : IPv4NextHop           : 0.0.0.0RouteMetric       : 1 Name                  : Test3AllUserConnection     : TrueTunnelType            : L2tpAuthenticationMethod  : {Pap}EncryptionLevel       : OptionalSplitTunneling        : FalseDnsSuffix             : washington.contoso.comIdleDisconnectSeconds : 600 ServerAddress : RAS.Washington.Contoso.comFriendlyName  : Washington
  • Add-VpnConnectionRoute (6) adds an IPv4/IPv6 route to the profile; -RouteMetric sets the metric. Remove-VpnConnectionRoute (5) takes the same ConnectionName + DestinationPrefix.
  • Get-VpnConnection (6) without a name shows all profiles; Get-VpnConnectionTrigger (2) shows trigger apps and trusted networks.
  • New-VpnServerAddress (4) builds objects for -ServerList; Remove-VpnConnection (5) deletes profiles.

228.5 Set-VpnConnection, EAP, IPsec and proxy

PowerShell
Set-VpnConnection -Name "Test3" `  -AllUserConnection -TunnelType "L2tp" `  -EncryptionLevel "Optional" `  -AuthenticationMethod Pap `  -SplitTunneling $False `  -RememberCredential $False -PassThru
Output
WARNING: The currently selected encryptionlevel requires EAP or MS-CHAPv2 logon securitymethods. Data encryption will not occur forPap or Chap.
  • Set-VpnConnection (23): like Add, but switches take $true/$false; errors if the profile does not exist; Pap/Chap means no data encryption.
  • New-EapConfiguration (18): builds EAP XML for -EapConfigXmlStream; -Tls -VerifyServerIdentity, -Ttls, -Peap with -TunneledEapAuthMethod, -FastReconnect, -EnableNap.
  • Set-VpnConnectionIPsecConfiguration (14): IPsec crypto for IKEv2 and L2TP only; -RevertToDefault restores defaults.
  • Set-VpnConnectionProxy (8): proxy for the VPN; docs: used only on a forced (full) tunnel.
PowerShell
$A = New-EapConfiguration -Tls `  -VerifyServerIdentity -UserCertificateSet-VpnConnectionIPsecConfiguration `  -ConnectionName "Contoso" `  -AuthenticationTransformConstants None `  -CipherTransformConstants AES256 `  -EncryptionMethod AES256 `  -IntegrityCheckMethod SHA384 `  -PfsGroup None -DHGroup ECP384 `  -PassThru -ForceSet-VpnConnectionProxy -ConnectionName `  "Contoso" -ProxyServer "10.0.0.1:8080" `  -BypassProxyForLocal -PassThru
Output
AuthenticationTransformConstants : NoneCipherTransformConstants         : AES256DHGroup                          : ECP384IntegrityCheckMethod             : SHA384PfsGroup                         : NoneEncryptionMethod                 : AES256

228.6 Triggers: auto-connect

TriggerAdd / Remove / SetEffect
ApplicationAdd/Remove-VpnConnectionTriggerApplication (4/4)Launching the app (e.g. powershell.exe, Remote Desktop) connects the VPN
DNS configurationAdd (7) / Remove (4) / Set (7)Opening a resource in washington.contoso.com starts the VPN; no -DnsIPAddress = no trigger
Trusted networkAdd (4) / Remove (4) / Set (4)VPN does not start inside these networks (in the office); -DefaultDnsSuffixes copies DNS trigger suffixes
PowerShell
Add-VpnConnectionTriggerApplication `  -ConnectionName "Contoso" -ApplicationID `  "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe",  "Microsoft.RemoteDesktop_Contoso0987" -PassThruAdd-VpnConnectionTriggerDnsConfiguration `  -ConnectionName "Contoso" `  -DnsSuffix "washington.contoso.com" `  -DnsIPAddress "172.16.12.23" -PassThruAdd-VpnConnectionTriggerTrustedNetwork `  -ConnectionName "Contoso" -DnsSuffix `  "newyork.contoso.com","washington.contoso.com" `  -PassThruSet-VpnConnectionTriggerTrustedNetwork `  -ConnectionName "ContosoVPN" `  -DefaultDnsSuffixes -PassThru -Force

228.7 Linux equivalent: WireGuard lab

  • Lab: server 192.168.248.10 and client 192.168.248.20 run userspace wireguard-go; keys stay inside the containers.
  • wg set (endpoint + allowed-ips) = Add-VpnConnection profile; ip route add = Add-VpnConnectionRoute; ip route del = Remove-VpnConnectionRoute; ip link delete = disconnect.
Terminal
wireguard-go wg0wg set wg0 \  private-key /etc/wireguard/private.key \  peer $(cat /etc/wireguard/server.pub) \  endpoint 192.168.248.10:51820 \  allowed-ips 172.31.98.0/24,172.31.99.0/24 \  persistent-keepalive 25ip address add 172.31.98.2/24 dev wg0ip link set wg0 mtu 1420 upping -c 2 172.31.98.1ip route get 172.31.99.10ip route add 172.31.99.0/24 dev wg0ip route get 172.31.99.10ip route get 1.1.1.1
Output
$ ping -c 2 172.31.98.164 bytes from 172.31.98.1: icmp_seq=1 ttl=64 time=8.54 ms64 bytes from 172.31.98.1: icmp_seq=2 ttl=64 time=1.22 ms2 packets transmitted, 2 received, 0% packet loss, time 1001ms$ ip route get 172.31.99.10172.31.99.10 via 192.168.248.1 dev eth0 src 192.168.248.20 uid 0$ ip route add 172.31.99.0/24 dev wg0$ ip route get 172.31.99.10172.31.99.10 dev wg0 src 172.31.98.2 uid 0$ ping -c 3 172.31.99.103 packets transmitted, 3 received, 0% packet loss, time 2002ms$ ip route get 1.1.1.11.1.1.1 via 192.168.248.1 dev eth0 src 192.168.248.20 uid 0
Linux lab: from split to full tunnel
  1. 1.No office route: 172.31.99.10 goes via eth0 (outside the tunnel)

  2. 2.ip route add 172.31.99.0/24 dev wg0: office traffic via wg0

  3. 3.1.1.1.1 still via eth0 = split tunnel

  4. 4.allowed-ips 0.0.0.0/0 + routes 0.0.0.0/1 and 128.0.0.0/1 via wg0

  5. 5.1.1.1.1 now via wg0 = full tunnel

  6. 6.Endpoint 192.168.248.10 stays on eth0 so packets do not loop

Terminal
wg set wg0 \  peer $(cat /etc/wireguard/server.pub) \  allowed-ips 0.0.0.0/0ip route add 0.0.0.0/1 dev wg0ip route add 128.0.0.0/1 dev wg0ip route get 1.1.1.1ip route get 192.168.248.10ip link delete dev wg0ip route
Output
$ ip route get 1.1.1.11.1.1.1 dev wg0 src 172.31.98.2 uid 0$ ip route get 192.168.248.10192.168.248.10 dev eth0 src 192.168.248.20 uid 0$ ip link delete dev wg0$ ip link show wg0Device "wg0" does not exist.$ ip routedefault via 192.168.248.1 dev eth0192.168.248.0/24 dev eth0 proto kernel scope link src 192.168.248.20

228.8 Common mistakes

  • Believing 'VPN connected = everything encrypted': in a split tunnel only office routes go through the tunnel.
  • Turning on SplitTunneling but forgetting Add-VpnConnectionRoute: office traffic goes outside and never reaches the file server.
  • Expecting Set-VpnConnectionProxy to apply on a split tunnel; it is used only on a full tunnel.
  • Choosing Pap/Chap with encryption: Windows warns that data encryption will not occur; prefer EAP or MS-CHAPv2.

228.9 Interview tips

  • Only office traffic in the tunnel: Add-VpnConnection -Name -ServerAddress -TunnelType Ikev2 -SplitTunneling, then Add-VpnConnectionRoute for office prefixes.
  • Verify with Get-VpnConnection; use -AllUserConnection for a profile all users need.
  • Split = routed prefixes only, lower gateway load; full = all traffic, office policy everywhere, more latency.
  • Triggers: app, DNS suffix and trusted network decide when the VPN auto-connects.
  • Linux parallel: WireGuard wg set, ip route add/del, ip link delete.