What It Does
This script changes nothing until you set DryRun=false and Approve=disable-usb-storage. Before that it prints the settings it would write. It refuses to make more than 25 changes in one run unless you raise MaxChanges.
Mode Block, with no AllowedSerials, sets USBSTOR Start to 4 and UASPStor Start to 4 where it exists, because USB 3 drives that use UASP bypass USBSTOR. It also sets the Removable Disks deny read and write policies. It does not use the deny for all removable storage, so CD and DVD drives, tape drives and phones that connect as MTP keep working.
Mode ReadOnly denies write access only, so a drive can be read and not written, and it undoes a full block set earlier. AllowedSerials, in Block mode only, uses device installation policy to block USB mass storage and allow the device instance IDs it can match to those serials. A drive must already be known to Windows. Unseen serials warn and stay blocked. Some UASP drives may also stay blocked because the script does not read their SCSI instance IDs.
Before it changes anything it saves the policy keys with reg export. A policy applies when a drive is plugged in, so a drive connected now stays mounted until it is unplugged or the PC restarts. A domain Group Policy that sets the same values overwrites this script at its next refresh. On Windows Server it refuses unless Force is true.
Before You Run It
- Run test-usb-storage-blocked first to see which USB drives Windows has already seen, with their serial numbers.
- Blocking USB storage also stops USB backup drives, backup software that copies to a USB disk, and scanners that save to a stick. The run warns about it.
- Plug in each drive you want in AllowedSerials before the run, then confirm the exception works. Some UASP drives use IDs the script cannot match.
- Run it once as a dry run and read the list, then run it for real.
Type and Risk
- Type: Action. It makes its change, checks the change worked and says what changed. It exits 0, or 2 when it could not do the job.
- Risk: High. It is a dry run unless you set both DryRun=false and Approve=disable-usb-storage, and it stops before making more than 25 changes in one run unless you raise MaxChanges. Run it once as a dry run and read the list first.
- What it changes: Writes the removable-storage and device-installation policies and sets the USBSTOR and UASPStor driver start types; backs the keys up first.
- Restart: It never restarts the machine itself. When a restart is needed to finish, the answer says so.
- Typical run: Under 20 seconds.
- Example answer: "USB storage blocked (USBSTOR disabled, removable disks deny read and write); 1 USB drive connected now stays mounted until unplugged or restart"
- Tested before listing on Windows 11 with Windows PowerShell 5.1, run as SYSTEM, against its own test cases.
Inputs
Pass an input as an argument (-Name value), as an environment variable with the same name, which is how most RMMs pass script variables, or change its default in the param() block at the top of the script. An argument wins over an environment variable, which wins over the default.
- Mode: Block (default) or ReadOnly.
- AllowedSerials: Serial numbers of USB drives that stay usable, separated by commas or semicolons, letters, digits, dot, underscore and hyphen only, 4 to 64 characters each, no wildcards. Mode Block only. Default empty.
- Force: true to allow the change on Windows Server (default false: refuse). A server's USB disks are often its backup targets.
Every script also takes DryRun, OutputFormat (Text, Answer, Json or Csv), ExportPath and ResultField, which writes the answer to an RMM custom field.
License
Provided "as is" under the MIT License, without warranty of any kind. Test before production use. MSP Outpost and User First Digital Inc. accept no liability for any damage or loss.