I want to make a connection from my laptop to my server where I have my music library. I used

net use "\\SERVER\Folder\etc\" /SAVECRED /PERSISTENT:yes

to connect to my server. But I also want to be able to connect to the server when I'm not at home, so I've set up a VPN server where I can manually connect to.

Now I'd like to be able to connect to the server without having to check if the VPN is enabled and whether or not I need to use the VPN.

Can I make it so that when I try to connect to my server, it connects to the VPN if I'm not on LAN, otherwise it just connects over LAN without the VPN?


For future reference, here's my final solution:

@echo off

ping -n 1 SERVER
if errorlevel 1 (
    rasdial "Server VPN" "username" "supersecretpassword"

    ping -n 1 SERVER
    if errorlevel 1 (
        echo msgbox "Could not establish connection to \\SERVER\" + vbCrLf + vbCrLf + "*sadface*" > %tmp%\tmpmsgbox.vbs
        wscript %tmp%/tmpmsgbox.vbs
        del %tmp%/tmpmsgbox.vbs

1 Answer 1


Would this work for you?

ping -n 1 SERVER
if errorlevel 1 rasdial vpn.server.com username password
net use "\\SERVER\Folder\etc" /SAVECRED /PERSISTENT:yes
  • The net use ... line is a one-time action (/PERSISTENT:yes), but thanks, I'll check if that works as soon as I'm away from my home network! Feb 6, 2014 at 23:29
  • Thanks, it works (I tested it with another server and another VPN). The only difference is that instead of using the VPN address, you have to use the name of the VPN you've added to the network adapters via the network center in the Control Panel (e.g. resdail "My VPN" "username" "password"). Thanks! Feb 6, 2014 at 23:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.