Automatic set “Register this connection’s addresses in DNS” for a VPN connection

Using PowerShell to create a VPN connection on a Windows 10 is quite easy. I used this command to create a VPN connection to a Meraki router.

$ServerAddress = "x.x.x.x"
$ConnectionName = "XXX"
$PresharedKey = "XXX"
Add-VpnConnection -Name "$ConnectionName" -ServerAddress "$ServerAddress" -RememberCredential -TunnelType L2tp -AllUserConnection -L2tpPsk "$PresharedKey" -AuthenticationMethod Pap -Force

But what about if you want to add the clients VPN IP adress in your DNS?

Default “Register this connection’s addresses in DNS” for a VPN connection is not enabled and it is not possible to enable with the Add-VpnConnection PowerShell command.

Off course you can set it manually but if you want to do it automatically and deploy the setting you have to edit the Remote Access Phonebook with PowerShell

If it is a -alluserconnection the phonebook can be found here: “C:\ProgramData\Microsoft\Network\Connections\Pbk\rasphone.pbk”

If it is a per user connection the phonebook can be found here: “C:\Users\USERNAME\AppData\Roaming\Microsoft\Network\Connections\Pbk\rasphone.pbk”

Use this Powershell command to set the setting:

$RASPhoneBook = "C:\ProgramData\Microsoft\Network\Connections\Pbk\rasphone.pbk"
(Get-Content $RASPhoneBook) -Replace 'IpDnsFlags=0', 'IpDnsFlags=3' | Set-Content $RASPhoneBook

1 thought on “Automatic set “Register this connection’s addresses in DNS” for a VPN connection”

  1. Thanks for the post. It really helped me out. I needed to adjust the per user connection and modified your code needed to store the data to the variable. By using the environment variable, I am able to use on any user bypassing the need to hard code a path.

    $RASPhoneBook = $env:APPDATA+”\Microsoft\Network\Connections\Pbk\rasphone.pbk”

    Reply

Leave a Comment