Introduction: VBScript Drive Lock

About: Hello, my name is xp4xbox, a really old name that really has nothing to do with xbox consoles. Anyway I enjoy making programs and have several instructables on some of them. So far, I know VBScript, Batch, Pow…

Update: This program now has the ability to hide the specified locked drives.

After making my screen lock which locks the users computer I decided to take the challenge of making a drive lock that locks a drive.

We all sometimes want to keep users out of a drive (especially a usb drive). So this program will lock the drive to prevent unauthorized users from gaining access to it.

Unlike my screen lock this script is pretty complicated. Mainly because I added so many if's and then's just to give to program a professional feel.

This program was made in VBScript.
In the next few steps I'll show you how I made it.

Step 1: My Program...

You can download my Drive Lock below:

Here is the raw VBScript file LINK. You will have to remove one of the 's' at the end so it is 'DriveLock.vbs' or what ever you want as long as it ends in .vbs.

Step 2: Making the Drive Lock (part 1)

Here I will show you the first part of the drive lock...The first pic is what comes up when you first run the program.

I made it so you have three options here, to lock a drive, to unlock drive(s) or exit.

Now here is how I did it:

Function DisplayPrompt()
    intSplash = MsgBox("What would you like to do?" & vbCrLf & vbCrLf  _     & "[Click on YES to lock a drive] " & vbCrLf _      & "[Click on NO to unlock drive(s)]",35, cTitleBarMsg)     If intSplash = 2 Then         DisplaySplashScreen()     ElseIf intSplash = 7 Then         On Error Resume Next         objWshShl.RegDelete "HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\NoViewOnDrive"         objWshShl.RegDelete "HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\NoDrives"         If Err.Number <> 0 Then             MsgBox "Drives are already unlocked.",16,cTitleBarMsg             DisplayPrompt()         End If         On Error Goto 0         objWshShl.Run "Taskkill /f /im explorer.exe",0         WScript.Sleep 300         objWshShl.Run "cmd /c explorer.exe",0         MsgBox "Drive unlocked was succesfull!",64,cTitleBarMsg         DisplayPrompt()     End If End Function

If you click NO to unlock the drive(s), it deletes the registry keys that contains the settings that locks/hides the drive it also checks to see if the drive is already unlocked. Then it restarts explorer.exe so that the changes take effect immediately.

If you click cancel, it displays the splash screen and then exits.

Finally if you click YES then it goes to the next menu. Which I will explain in the next step.

Step 3: Making the Drive Lock (part 2)

Next I will show you the menu were you choose witch drive you want to lock (pic 1).

Set colDrives = objFSO.Drives
For Each objDrive in colDrives
    strDriveList = strDriveList & objDrive.DriveLetter & Space(10)
strDrives = LCase(Replace(strDriveList," ","",1,-1))
Set colDrives = objFSO.Drives
strDriveList = ""
For Each objDrive in colDrives
    strDriveList = strDriveList & objDrive.DriveLetter & ":\" & Space(5)
Sub InputMenu
    strChoice = InputBox("Enter letter of the drive you wish to lock." & _
     " Or type ALL to lock all drives." & _
       vbcrlf & vbcrlf & "Available drives" & Space(3) & _
         ":" & vbCrLf & vbCrLf & strDriveList, cTitleBarMsg)

The first bit of code generates a list of all the available drives. The next part is the inputbox were you enter the letter of the drive that you would like to lock.

If you try typing in anything but a valid drive letter, and the program will refuse it. Here is how I did that:

If IsEmpty(strChoice) Then
    ElseIf strChoice = "" Then
        MsgBox "Do not leave this blank.",16, cTitleBarMsg
    ElseIf LCase(strChoice) = "all" Then
        'Do Nothing
    ElseIf Len(strChoice) <> 1 Then 
        MsgBox "You must enter the letter ONLY.",16, cTitleBarMsg
    ElseIf Not InStr(1,strDrives,LCase(strChoice),1) <> 0 Then 
        MsgBox "Invalid choice, please try again.",16, cTitleBarMsg
    End If

The first two lines check to see if the user hit the cancel button, and if they did the program exits.

The next three lines checks to see if the user left the input area blank.

The rest of the code is pretty confusing, but it is basically just making sure that the user typed in a valid drive.

Step 4: Making the Drive Lock (Part 3)

For some reason the registry cannot read the drive letter alone, so it must be put into a corresponding integer. That is what this code below is doing. For more information on this click here.

 ElseIf strChoice = "a" Then
     intDriveNumber = 1
 ElseIf strChoice = "b" Then
     intDriveNumber = 2
 ElseIf strChoice = "c" Then
     intDriveNumber = 4

Once the integer is configured the script can now write to the registry using this code:

Another note, is that this program writes to HKLM instead of HKCU. HKLM affects all users instead of just the current logged on user so that the lock is more effective.

objWshShl.RegWrite "HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\NoViewOnDrive", intDriveNumber, "REG_DWORD"

objWshShl.RegWrite "HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\NoDrives", intDriveNumber, "REG_DWORD"

Step 5: Drive Lock

I hope you found this instructable helpful.
if you don't understand a part of this, please post a comment or pm me.

And please give me feedback on any problems and please rate. I spent a lot of time on this.

If you want to download the raw vbs file, just make sure it is saved with a .vbs file extension. Another nice thing about it, is that it is portable so you can run it off a flash drive and you don't need to install anything.