Created
April 28, 2023 21:42
-
-
Save pavel-a/1bdc71c58d22f4a72f68ea60345effac to your computer and use it in GitHub Desktop.
Windows; toggle write protection on inserted USB drives (wsh)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//----------------------------------------------------------------------------- | |
// Toggle write protection on USB drives | |
// | |
// See http://support.microsoft.com/kb/555443 | |
// [ HKLM\System\CurrentControlSet\Control\StorageDevicePolicies\WriteProtect: reg_dw: 1 ] | |
// | |
// NOTES: | |
// - This registry hack (KB555443) works only starting from XP SP2; I don't check for the SP. | |
// - If a drive is already attached, write enable seems to come in effect immediately; | |
// Write disable becomes effective only after the drive is re-attached. | |
// In any case, reboot is NOT needed. | |
// - You must be admin to change this setting, otherwice, registry write will fail. | |
// - A group policy update can clobber this parameter | |
// - Script popup prompts will auto cancel on timeout. | |
//----------------------------------------------------------------------------- | |
// Change log: | |
// 8/14/2006 pa01 created: jscript v.5.6. Run with wscript. | |
//----------------------------------------------------------------------------- | |
function do_kb_555443() | |
{ | |
var regp = "HKLM\\System\\CurrentControlSet\\Control\\StorageDevicePolicies\\WriteProtect"; | |
var v = 0; | |
var found = false; | |
var nn = 0; | |
var TIMEOUT=10; | |
var PROMPT1= "USB drives writable policy"; | |
var osh = new ActiveXObject("WScript.Shell"); | |
function Echo(s) { WScript.Echo(s); }; | |
function Msgbox( str, style ) { return osh.Popup( str, TIMEOUT, PROMPT1, style ); }; | |
// Echo("go" ); | |
while(true) { // Loop until user cancels the prompt | |
try { | |
v = osh.RegRead( regp ); | |
found = true; | |
} catch(e) { | |
if( 2 == (e.number & 0xFFFF)) { | |
v = 0; // Not found. set = 0 | |
found = false; | |
} else { | |
Echo("Error:" + (e.number & 0xFFFF) + ":" + e.description ); | |
} | |
} | |
// Only values 0 and 1 are documented! | |
if( (v != 0) && (v != 1) ) { | |
nn = Msgbox( "WARNING! unknown value=" + v.toString() + " ! Continue?", 1+48 ); | |
if( 1 != nn ) return; | |
} | |
var vs = (v == 0)? "allowed":"protected"; | |
nn = Msgbox( "Write to external USB drives is " + vs + ". Toggle?", 1+32 ); | |
if( 1 != nn ) return; | |
// Toggle | |
v = (v == 0) ? 1 : 0; | |
// Write back: | |
try { | |
v = osh.RegWrite( regp, v, "REG_DWORD" ); | |
} catch(e) { | |
Echo("Error writing to the registry!:" + (e.number & 0xFFFF) + ":" + e.description ); | |
} | |
// Go read again | |
} //while | |
osh = 0; | |
} | |
// MAIN | |
do_kb_555443(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment