Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to create a script that maps a network drive whenever I connect to a VPN, then disconnects the drive when I disconnect from the VPN, using Task Scheduler to launch the script when the event is created.

It launches the script, which creates the drive, but when Powershell closes, it disconnects the drive, so it only stays open for a few seconds, then closes it again. I have the persist parameter specified, but it doesn't persist.

New-PSDrive -Name "N" -PSProvider FileSystem -Root \(Serverpath)\ndrive -Persist

share|improve this question
up vote 1 down vote accepted

How about using batch? It's quite simple.

Net Use DriveLetter UNCPath [Optional: /User:Domain\Username] [Optional: Password] [Optional: /Persistent:Yes\No]
Net Use T: \\Fileserver\Share /user:my-domain\myuser Pa$$w0rd /Persistent:Yes

After the VPN disconnects you can remove the net drive:

Net Use T: /Delete /Y

The "/Y" skips the confirmation.

share|improve this answer
I was able to get it to work with net use, but prefer Powershell, and with the help of someone on Facebook, was able to get it to use Powershell. – Davidw Oct 20 '13 at 19:08
Could you share the answer here so others can benefit? – Chris Trombley Sep 15 '14 at 17:31
On the actions tab of a scheduled task, in the parameter box, place this line: -Command "& New-PSDrive -Name N -Root \\ (serverpath) \ndrive -PSProvider FileSystem -Persist" – Davidw Sep 27 '14 at 1:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .