Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 would like an screen saver / logon script that checks if a network path is available and then map it to a unit. If it is not available it disconnects/don't connect.

Network path is \\drive1

Also I need to use username/password to connect to that path.

share|improve this question

You can use the exist command to check if the path is valid:

if exist \\\drive1 net use s: \\\drive1

If you need to provide credentials (i.e. your current Windows user doesn't have access to that share), add /user:

if exist \\\drive1 net use s: \\\drive1 /user:myDomain\myUser myPassword

If there's a chance that the share already exists, and you want to delete it if it's no longer available, add an else clause:

if exist \\\drive1 (net use s: \\\drive1) else (net use /delete s:)

And once again, add the /user if you need it.

You can tie this all together in a batch file similar to the following:

@echo off
if exist \\\drive1 (set shareExists=1) else (set shareExists=0)
if exist y:\ (set driveExists=1) else (set driveExists=0)
if %shareExists%==1 if not %driveExists%==1 (net use y: \\\drive1)
if %shareExists%==0 if %driveExists%==1 (net use /delete y:)
set driveExists=
set shareExists=
share|improve this answer
ok I am just pasting that in notepad and saving as .vbs but when I run a task of it it gives Visual basic error? – FernandoSBS May 21 '13 at 21:35
That command is a batch command - it should work from any standard batch file. – Geoff May 21 '13 at 22:28
C:\Windows>if exist \\\volume1 (net use y: \\\volume1 ) e lse (net use /delete y: ) The network connection could not be found. More help is available by typing NET HELPMSG 2250. – FernandoSBS May 21 '13 at 22:44
I'll add an edit... – Geoff May 22 '13 at 12:53
i'm sorry? (5 chars) – FernandoSBS May 22 '13 at 21:17

Powershell would make this easy:

If(Test-Path \\\Drive1)
    net use M: \\\Drive1 /user:Domain\UserName Password
else {net use M: /delete > nul}
share|improve this answer
ok I am just pasting that in notepad and saving as .vbs but when I run a task of it it gives Visual basic error? – FernandoSBS May 21 '13 at 21:35
Save it as a .ps1 and run it from Powershell. – AthomSfere May 21 '13 at 21:36
i'm not familiar with powershell, how can I automate it in Task Scheduler? – FernandoSBS May 21 '13 at 21:44
You should really split the 2 questions. This is a QA site. Also, google it if you have not already:… – AthomSfere May 21 '13 at 21:47
ok got it. What the bonus of using powershell? – FernandoSBS May 21 '13 at 22:02

It is just simplier to just try to map it using the Windows File explorer or using the net use command. Either it works or it doesn't.

share|improve this answer
If it's available I want to be automatically connected when I logon / screen saver, if it's not then I want it removed from the mapped drives. So what you suggested is not an option. – FernandoSBS May 21 '13 at 19:20
I don't understand, it you issue a net use, if the command succeeds it will be connected. If the command fails, then it is removed from the mapped drives. Seems simple to me. – mdpc May 21 '13 at 19:47

This is the final code:

function run{
net use
If(Test-Path \\\volume1)
    if (((New-Object System.IO.DriveInfo("Y:")).DriveType -ne "NoRootDirectory")) 
            "already mounted and accessible"
            net use Y: \\\volume1
else { 
    if (((New-Object System.IO.DriveInfo("Y:")).DriveType -ne "NoRootDirectory"))
            net use Y: /delete
exit 4


I use Test-Path \\\volume1 as suggested to check if the network path is available and ((New-Object System.IO.DriveInfo("Y:")).DriveType -ne "NoRootDirectory") to check if the drive letter exists.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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