USB Drive Keeps Opening by Itself and CMD Windows Flash, What to do?

Published by Nyau Wai Hoe - Updated on

If a File Explorer window for your USB drive keeps opening by itself and you see Command Prompt windows flashing open and closed on your Windows 11 or 10 PC, even though Microsoft Defender or Malwarebytes says your PC is clean, it’s most likely a USB worm that came with the flash drive. This guide will explain what’s going on and show you how to remove it from both your PC and the USB drive.

Also see: Unknown Program in Task Manager Startup Tab Windows 11/10

USB Drive Keeps Opening by Itself and CMD Windows Flash

Why does the USB drive keep opening and CMD windows flash?

This is the typical behavior of a type of malware often called a USB worm or “shortcut virus”. It’s been around for years, and Microsoft wrote about a newer one called Raspberry Robin in 2022. The signs are usually the same: the drive keeps popping open, CMD windows flash, and regular scans find nothing.

Here’s roughly how it works:

  • The infected USB drive has shortcut (.lnk) files that look like your folders, sometimes named after a USB brand. Your real files are still there, just hidden.
  • When you click one of those shortcuts, it runs Command Prompt (cmd.exe) in the background, which starts a script or installer stored on the drive. That’s the flashing CMD window you see.
  • Some versions also open the drive’s folder for you, so it looks like the shortcut did what you expected while the worm runs in the background. That’s the drive window that keeps popping up.
  • The worm then adds itself to startup entries or scheduled tasks so it runs again every time you sign in, and copies itself to other USB drives you plug in.

Scans don’t always catch it, especially the script-based versions. Even scanners like Malwarebytes and Microsoft Safety Scanner can miss it. Even when the files get deleted, the startup entry can be left behind, which keeps the CMD windows coming back.

Related concern: Chrome or Firefox keeps opening new tabs on its own, virus?

Unplug the USB drive first

Before anything else, unplug the infected USB drive and don’t open any shortcuts on it until it’s cleaned. Every click on one of those shortcuts can run the worm again. If you’ve plugged other USB drives into this PC lately, they might be infected too.

Run a Microsoft Defender Offline scan

A normal scan runs while Windows (and the malware) is running. The Microsoft Defender Offline scan restarts your PC and scans it without loading Windows, so malware that hides while Windows is running can’t hide as easily. Save any open work first, since your PC will restart. Here’s how to run it:

  1. Open Windows Security from the Start menu.
  2. Go to Virus & threat protection and select Scan options.
  3. Select Microsoft Defender Antivirus (offline scan).Microsoft Defender Antivirus offline scan Windows 11
  4. Click Scan now and accept any prompt that appears. Windows restarts, runs the scan (Windows Security says it takes about 15 minutes), and restarts again when it’s done.

After the scan, open Windows Security again and check Protection history to see if anything was found and removed.

Pro tip: How to Scan an .exe File for Viruses to Check If It’s Safe

Find and remove what keeps starting it

If the CMD windows still flash after the scan, something is still telling Windows to run the worm. Removing those startup entries by hand should stop it.

Check “Startup apps” in Task Manager

Here’s what to do:

  1. Press Ctrl + Shift + Esc to open Task Manager.
  2. Select Details on the left. If you see wscript.exe, right-click it and select End task, so the script isn’t still running while you clean up.
  3. Select Startup apps on the left.
  4. Look for anything you don’t recognize, especially entries with no publisher, or ones that run wscript, cmd, a .vbs or .js file, or something from your AppData folder.
  5. Right-click a suspicious entry and select Open file location to see the file it runs.Startup apps in Task Manager Windows 11
  6. Right-click the entry again and select Disable.

Restart your PC and see if the CMD windows still appear.

Look deeper with Autoruns

Task Manager doesn’t show everything. Autoruns, a free Microsoft Sysinternals tool, lists every place a program can start itself from, including the Run and RunOnce registry keys, the Startup folder and scheduled tasks.

  1. Download Autoruns from Microsoft at https://download.sysinternals.com/files/Autoruns.zip and extract the ZIP file.
  2. Right-click Autoruns64.exe and select Run as administrator.
  3. Click Agree on the license agreement the first time you run it.
  4. Open the Options menu and make sure Hide Microsoft Entries is ticked, so most of Windows’ own entries are hidden from the list.Autoruns hide Microsoft entries Windows 11
  5. Check the Everything, Logon and Scheduled Tasks tabs for entries that point to a script (.vbs, .js), to your USB drive’s letter, or to a random-looking file in AppData. Don’t panic over every cmd.exe line, though: Windows has a few of its own, signed by Microsoft, and they still show up with this option ticked.
  6. Uncheck a suspicious entry to disable it.
  7. Once you’re sure, right-click it and select Delete.
  8. Delete the file it pointed to.

Restart your PC and see if the CMD windows and the drive window are gone.

Clean the USB drive and get your files back

Once your PC is clean, you can deal with the drive itself. Your files are usually still on it, just hidden. The worm marks them as hidden system files, and the attrib command removes the hidden, system and read-only attributes it set.

  1. Plug the USB drive back in, but don’t open it by double-clicking anything on it.
  2. Note its drive letter in This PC (for example, E:).
  3. Search for “cmd” in the Start menu and open Command Prompt.
  4. Type the following command, replacing E: with your drive’s letter, and press Enter:
    attrib -h -r -s /s /d E:\*.*

    attrib command unhide files USB drive Windows 11

  5. Open the drive in File Explorer. Your folders should be visible again.
  6. Delete the shortcut (.lnk) files and any unknown .vbs or .js files on the drive.
  7. Copy your files off the drive.
  8. Format the drive to make sure nothing is left.

Do the same for every other USB drive that was plugged into this PC while it was infected.

See hidden files in File Explorer

If you just want to check what’s hidden on the drive, open File Explorer and select View > Show > Hidden items. Please note that files marked as both hidden and system files (which the worm does) still won’t show up this way, so you’ll still need the attrib command above.

Show hidden items File Explorer Windows 11

Learn more: How to Show Hidden File Name Extensions in Windows 10 File Explorer

Turn off AutoPlay

Microsoft recommends turning off autorun for removable drives to stop this kind of worm. On a home PC, the closest setting is AutoPlay:

  1. Open Settings and go to Bluetooth & devices > AutoPlay.
  2. Turn off Use AutoPlay for all media and devices.Turn off AutoPlay Windows 11

This way, Windows won’t open or run anything by itself when you plug in a USB drive.

Reset Windows as a last resort

If the CMD windows keep coming back no matter what you remove, something may still be hidden that these steps can’t find. At that point, back up your personal files (not programs, and not from the infected drives), and reset Windows. For detailed instructions, read: How to Reset Windows 11/10 to Factory Default.


Nyau Wai Hoe
Nyau Wai Hoe is the Founder and Chief Editor of WindowsDigitals.com. With a degree in software engineering and over 12 years of experience in the tech support industry, Nyau has established himself as an expert in the field, with a primary focus on the Microsoft Windows operating system. As a tech enthusiast, he loves exploring new technologies and leveraging them to solve real-life problems.

Share via
Copy link