VBScript to Disconnect Network Drive
Your logon scripts gain two benefits from RemoveNetworkDrive. Firstly, preventing errors when you are trying to map a network drive that’s already connected. Secondly, removing an existing network drive is particularly useful when your scripts are at the testing stage and you don’t want to keep going to explorer, and clicking disconnect.
Topics for RemoveNetworkDrive
- Disconnect Network Drive Scenario
- RemoveNetworkDrive Arguments
- Example 1 – Simple RemoveNetworkDrive
- Example 2 – Full RemoveNetworkDrive Syntax
- Summary
♦
Disconnect Network Drive Scenario
For me, the situation is that I am testing a script that maps a network drive, when I run the script for a second time, I get an error message. Now I could use On Error Resume Next, but that may mask other problems with the script, so I add a RemoveNetworkDrive statement. My friend ‘Barking’ Eddie love logoff scripts, and he includes a RemoveNetworkDrive in his tidy-up routine.
RemoveNetworkDrive Arguments
Normally, you just need two elements, the method, RemoveNetworkDrive and the drive letter.
objNetwork.RemoveNetworkDrive "H:" or
objNetwork.RemoveNetworkDrive strDriveLetter
Mastering the basics of RemoveNetworkDrive are easy. If there is a danger it’s what I call ‘over think’. You do not need a comma before the drive letter, and you do not need the name of the network share. What you do need is – just the plain drive letter.
Even mastering the advanced arguments is not difficult, just watch out for the commas, here is the full syntax:
RemoveNetworkDrive strDriveLetter, bForce, bUpdateProfile
The b in bForce and bUpdateProfile means Boolean, this helps us by knowing that we should just add true or false. bForce means remove the drive even if its in use. (Rather like ‘coming ready or not’ in hide and seek.)
Setting bUpdateProfile to true tells VBScript to update the user’s profile.
Example 1 – Simple RemoveNetworkDrive
Even though RemoveNetworkDrive is easy, we need preparation in the form of drive that has already been connected. Otherwise there would be nothing to remove and all you would get is Error: ‘This network connection does not exist’
- You need an H: drive before the script has any network drive to remove.
‘ AddDrive.vbs –
‘ VBScript – Add drive to Practice RemoveNetworkDrive.
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 1.2- April 24th 2010
‘ —————————————————-‘
Option Explicit
Dim objNetwork, strDriveLetter, strNetworkPath
strDriveLetter = "H:"
strNetworkPath = "\\alan\home"
Set objNetwork = CreateObject("WScript.Network")
‘ Section which maps strDriveletter to strNetworkPath
objNetwork.MapNetworkDrive strDriveLetter, strNetworkPath
Wscript.Quit
‘ End of Script
Instructions to RemoveNetworkDrive
- Copy and paste the script below into notepad or get a script editor such as OnScript (free download).
- Save the file with .vbs extension e.g. RemoveNetworkDrive.vbs.
- Double click and check in your Windows Explorer. There should be no H:\
‘ RemoveNetworkDrive.vbs
‘ VBScript to disconnect network drive H:
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 1.3 – May 2012 (Update thanks to Glenn Dhooghe)
‘ ————————————————————-‘
Option Explicit
Dim objNetwork, strDriveLetter
strDriveLetter = "H:"
Set objNetwork = CreateObject("WScript.Network")
‘ Section which removes strDriveLetter
objNetwork.RemoveNetworkDrive strDriveLetter
Wscript.Quit
‘ Guy’s Script ends here
VBS Learning Points
Note 1: The basic RemoveNetworkDrive is a simple command with no commas and only one argument – the drive letter.
Note 2: Surprisingly, you do not need the network path.
Guy Recommends SolarWinds’ Free Network Monitor
Thus utility makes it easy to check the health of a router or firewall. Check the real-time performance, and availability statistics, for any device on your network. Get started with an extensive collection of "out-of-the-box" monitors for popular network devices. Give Network Monitor a whirl – it’s free. Download your free Network Device Monitor
If you need more comprehensive network analysis software:
Download a free trial of NPM (Network Performance Monitor)
Example 2 – Full RemoveNetworkDrive Syntax
Just in case you need the bForce or the bUpdateProfile switch here is an example for the full RemoveNetworkDrive Command.
Pre-requisites
- This is a preliminary script to create an H: drive. My point is the main script needs a mapped drive to exist, before RemoveNetworkDrive can remove it!
- To get the full effect of bForce, try opening the mapped networkdrive with Windows Explorer, now try Example 1. Almost certainly, you got an error message. Try again but this time with Example 2 and its extra bForce command.
‘ AddDrive.vbs –
‘ VBScript – Add drive to Practice RemoveNetworkDrive
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 1.3 – April 24th 2010
‘ —————————————————-‘
Option Explicit
Dim objNetwork, objShell
Dim strDriveLetter, strNetworkPath, strExplorer
strDriveLetter = "H:"
strNetworkPath = "\\alan\home"
Set objNetwork = CreateObject("WScript.Network")
‘ Section which removes strDriveLetter
objNetwork.MapNetworkDrive strDriveLetter, strNetworkPath
Set objShell = CreateObject("WScript.Shell")
strExplorer = "Explorer" & " " & strDriveLetter
objShell.run (strExplorer)
Wscript.Quit
‘ End of Script
Instructions to Remove Network Drive
- Copy and paste the script below into notepad.
- Save the file with .vbs extension e.g. RemoveNetworkDrive.vbs.
- Double click and check in your Windows Explorer. There should be no H:\
‘ RemNetDrivebForce.vbs – Windows Logon Script
‘ VBScript to Remove Network Drive H: with bForce
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 1.3- April 24th 2010
‘ —————————————————–‘
Option Explicit
Dim objNetwork, objShell, strDriveLetter
Dim bForce, bUpdateProfile
strDriveLetter = "H:"
bForce = "True"
bUpdateProfile = "True"
err.number = vbempty
Set objNetwork = CreateObject("WScript.Network")
‘ Removes strDriveLetter, with bForce, pUpdate Profile
On Error Resume Next
objNetwork.RemoveNetworkDrive strDriveLetter, _
bforce, bUpdateProfile
‘ Error correcting code
If err.number = vbEmpty then
Set objShell = CreateObject("WScript.Shell")
objShell.run ("Explorer")
ElseIf err.number = -2147022646 then
Wscript.Echo "You must create " & strDriveLetter
Else Wscript.echo "Unknown " & err.number
End If
WScript.Quit
‘ End of Guy’s Script
Learning Points
Note 1: Spot the bForce, and bUpdateProfile commands, true, true.
Note 2: To test these extra RemoveNetworkDrive arguments, open a mapped network drive. Now try running the script with and without the bForce.
Note 3: The bonus part of this script is the error correcting code. Take the time to see how the If … then, end if construction works. Also the objShell Run, which opens the Explorer.
Guy Recommends: Permissions Analyzer – Free Active Directory Tool
I like thePermissions Monitor because it enables me to see quickly WHO has permissions to do WHAT. When you launch this tool it analyzes a users effective NTFS permissions for a specific file or folder, takes into account network share access, then displays the results in a nifty desktop dashboard!
Think of all the frustration that this free utility saves when you are troubleshooting authorization problems for users access to a resource. Give this permissions monitor a try – it’s free!
Download Permissions Analyser – Free Active Directory Tool
Summary of RemoveNetworkDrive
RemoveNetworkDrive is a handy VBScript method for those times when you want to delete a network drive letter. If you want to prevent error messages when you run a test script for a second time, then just include a RemoveNetworkDrive command to roll back the MapNetworkDrive command.
Download my Logon Script eBook for only $6.25
The extra features you get in your eBook include, more pages full of detailed examples. Also, ten ‘how to…’ sections, with screen shots showing which menus to use. Go for Guy’s eBook – and get a printable version with copy enabled and no expiry date.
If you like this page then please share it with your friends
See more logon scripts examples
• Logon Script Home • Disconnect Network Drives • Map Network Drive Group • Free CSV Tool
• Map Network Drive Script • Vbscript Map Network Drive Username • Add Error Correcting Code
• ObjNetwork.MapNetworkDrive • Multiple Mapped Network Drives •Troubleshooting logon scripts