How to disconnect a network drive using vbscript

Below is a script to disconnect a network drive using vbscript. We employ the services of WScript.Network object to do the same 🙂 Enjoy!. There are two functions below. One is the actual disconnect drive and the other one is to parse the command line arguments. We want to make sure the drive letter that has to be disconnected, is specified as an input parameter to the script.

'FileName : disconnectdrive.vbs
'Purpose  : To disconnect specified network drive
'Usage    : Usage  : cscript //nologo disconnectdrive.vbs <drive>
'To Debug : cscript //D //X //nologo disconnectdrive.vbs <drive>
'Example : Usage  : cscript //nologo disconnectdrive.vbs i:
'Date     : 08/01/2008

Function disconnectdrive()
set objNetwork = WScript.CreateObject("WScript.Network")
Set ObjFSO = CreateObject("Scripting.FileSystemObject")
strDrive = WScript.Arguments(0)

if(ObjFSO.DriveExists(strDrive)) then
objNetwork.RemoveNetworkDrive strDrive, True, True
WScript.Echo "Disconnected network drive : " & strDrive
end if

Set ObjFSO = nothing
Set objNetwork = nothing
End function

Function parseArgs()
Dim filelen
filelen = WScript.Arguments.length
if filelen < 1 Then
WScript.Echo("Usage  : cscript //nologo disconnectdrive.vbs <drive>")
WScript.Echo("Example : cscript //nologo disconnectdrive.vbs i:")
end if
End function



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s