Introduction to RemoveNetworkDrive
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
♦
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.
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.
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 http://computerperformance.co.uk/ ' Version 1.2- April 24th
2005 ' -----------------------------------------------------------------' Option Explicit Dim objNetwork, strDriveLetter, strNetworkPath strDriveLetter = "H:" strNetworkPath = "\\alan\home"
Set objNetwork = CreateObject("WScript.Network") ' Section which removes strDriveLetter 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 - Remove Drive H: ' Author Guy Thomas http://computerperformance.co.uk/ ' Version 1.2- April 24th 2005 '
----------------------------------------------------------------' 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 LANSurveyor
LANSurveyor will produce a neat diagram of your network topology. But that's
just the start;
LANSurveyor can
create an inventory of the hardware and software
of your machines and network devices. Other neat features include dynamic
update for when you add new devices to your network. I also love the ability to export
the diagrams
to Microsoft Visio.
Finally, Guy bets that if you take a free trial of LANSurveyor then you will
find a device on your network that you had forgotten about, or someone else
installed without you realizing!
Download a Free Trial of LANSurveyor
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 http://computerperformance.co.uk/ ' Version 1.3 - April 24th
2005 ' -----------------------------------------------------------------' 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 RemoveNetworkDrive - 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 Drive H: with bForce ' Author Guy Thomas http://computerperformance.co.uk/ ' Version
1.3- April 24th 2005 ' -----------------------------------------------------' 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.
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.
See Also● Logon Script Home ●
MapNetworkDrive ● EnumNetworkDrives ●
AddWindowsPrinterConnection |