This has been tested under Windows 7 and Windows 10. For Windows 10, make sure the script is compiled as, ran as, 64 bit.
W10 Screenshot of Registry
W10 Screenshot of Explorer (D drive is hidden)
The Code (save as script.vbs – run as admin)
On error resume next 'THIS WILL CREATE A REG KEY FOR THE SPECIFIED DRIVE LETTER 'REG KEY WILL LOOK SOMETHING LIKE THIS 'NODRIVES REG_DWORD 0X00000008 'SET DRIVE LETTER TO HIDE Const Drive = "D" 'CALL SUBROUTINE HideDriveLetter() '---------------------------- Sub HideDriveLetter() On Error Resume Next Dim objShell, RegKeyPath, DriveBits, substr Dim fso, d, drives, s, count Dim ReturnCode Dim x, i , j ReturnCode = 0 DriveBits = 0 Set objShell = CreateObject("WScript.Shell") RegKeyPath = "HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\NoDrives" Set fso = CreateObject("Scripting.FileSystemObject") Set drives= fso.Drives If UCase(Drive) = "SHOWALL" Then 'DELETE WRITE REGISTRY, SET SYSTEM DEFAULT objShell.RegDelete RegKeyPath If Err = 0 Then Call ReloadExplorer() Err.clear Else 'msgbox "Admin access required!" Err.clear End If Else For i = 1 To Len(Drive) substr= Mid(Drive,i,1) x = 0 For j = Asc("A") To Asc("Z") If Chr(j) = UCase(substr) Then 'VERIFY INPUT If InStr(Drive,substr) = InStrRev(Drive,substr) Then 'VERIFY LETTER If fso.DriveExists(substr) Then DriveBits = DriveBits + 2^x Else 'msgbox "Drive does not exist!" ReturnCode = 1 Exit For End If Else ReturnCode = 1 End If End If x = x + 1 Next If ReturnCode = 1 Then Exit For End If Next 'VALIDATE RETURN CODE Dim strDriveDriveBits 'READ REGISTRY KEY strDriveDriveBits = objShell.RegRead(RegKeyPath) Err.clear If ReturnCode = 0 And Drive <> "" Then If DriveBits = 0 Then 'msgbox "Missing drive letter!" Call HideDriveLetter() Else If strDriveDriveBits = DriveBits Then 'msgbox "Drive has been hidden!" Else 'Write Registry objShell.RegWrite RegKeyPath, DriveBits, "REG_DWORD" If Err = 0 Then Call ReloadExplorer() Else 'msgbox "Admin access required!" Err.clear End If End If End If ElseIf ReturnCode = 1 Then Call HideDriveLetter() End If End If End Sub Function ReloadExplorer() Dim strComputer, objWMIService, colProcess, objProcess strComputer = "." Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = 'explorer.exe'") 'KILL/RELOAD For Each objProcess in colProcess objProcess.Terminate() Next End Function
* to unhide a drive letter, simply delete the reg key and reload explorer.exe
Update 01/24/2017
If you would like to ‘automate’ the drive detection, so…the script determines which drive letter should be hidden, add the following code to the top. You will need some kind of marker file or folder. I used MyFolder.
Figure out by previous marker
‘AUTOMATICALLY DETERMINE DRIVE LETTER TO HIDE
dim fso
Set fso = CreateObject(“Scripting.FileSystemObject”)
if (fso.FolderExists(“D:\MyFolder”)) then AutoDrive = “D”
if (fso.FolderExists(“E:\MyFolder”)) then AutoDrive = “E”
if (fso.FolderExists(“F:\MyFolder”)) then AutoDrive = “F”
if (fso.FolderExists(“G:\MyFolder”)) then AutoDrive = “G”
if (fso.FolderExists(“H:\MyFolder”)) then AutoDrive = “H”
‘SET DRIVE LETTER TO HIDE
Drive = AutoDrive
Figure out drive letter by label
strComputer = “.”
DriveLetter = “”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)
Set colItems = objWMIService.ExecQuery (“Select * From Win32_LogicalDisk”)
For Each objItem in colItems
If objItem.VolumeName = “KINGSTON” then DriveLetter = objItem.DeviceID
Next
msgbox DriveLetter