If you've ever tried to delete a node_modules folder on Windows, you know the pain: endless file path errors, permission issues, and Windows Explorer freezing. Enter rimraf - a cross-platform tool that removes files and directories without mercy.
In this tutorial, I'll show you how to create a PowerShell script that adds a "Delete with rimraf" option directly to your Windows context menu, complete with a custom icon.
What is Rimraf?
Rimraf is a Node.js package that provides the Unix command rm -rf functionality across all platforms. It's especially useful for deleting deeply nested directories like node_modules, .next, or dist folders that Windows struggles with.
Prerequisites
- Windows 10 or 11
- Node.js installed (for npx)
- Administrator privileges (required for registry modifications)
The Script
Let's build a PowerShell script that:
- Downloads a delete icon and stores it in
%USERPROFILE%\.actions\rimraf - Creates a batch file to execute rimraf
- Registers context menu entries for files, folders, and folder backgrounds
Create a file named setup-rimraf-context-menu.ps1:
How It Works
1. Administrator Check
The script first verifies it's running with administrator privileges using WindowsBuiltInRole::Administrator. Registry modifications require elevated permissions.
2. Directory Setup
Creates the .actions\rimraf directory in your user folder to store:
delete_icon.ico- The context menu iconrimraf.bat- The batch file that executes rimraf
3. Batch File
The batch file is simple but effective:
npx automatically downloads and runs rimraf@6 if it's not installed. The %1 placeholder is replaced with the selected file or folder path.
4. Registry Modification
The script uses reg.exe (Windows Registry Editor command-line tool) instead of PowerShell cmdlets. This approach avoids issues with special characters like * in registry paths.
Three context menu entries are created:
- Files (
HKCR\*\shell\rimraf) - Right-click any file - Folders (
HKCR\Directory\shell\rimraf) - Right-click any folder - Folder Background (
HKCR\Directory\Background\shell\rimraf) - Right-click inside a folder
Installation
-
Save the script as
setup-rimraf-context-menu.ps1 -
Right-click the script and select "Run with PowerShell" or "Run as Administrator"
-
The script will:
- Create the
.actions\rimrafdirectory - Download the delete icon
- Create the batch file
- Register context menu entries
- Create the
-
If needed, restart Windows Explorer:
Usage
After installation, simply right-click on any file or folder and select "Delete with rimraf". A command prompt will appear showing the deletion progress.
Common Use Cases
Delete node_modules:
Right-click node_modules → Delete with rimraf
Delete build folders:
Right-click dist/, .next/, or build/ → Delete with rimraf
Delete stubborn files:
Right-click any file with path length issues → Delete with rimraf
Customization
Change the Icon
Replace the $iconUrl variable with your preferred icon URL, or manually place an .ico file in the .actions\rimraf directory.
Change the Label
Modify the "Delete with rimraf" string in the reg add commands to your preferred text:
Use a Different Tool
Replace npx rimraf@6 in the batch file with any command:
Why This Approach?
You might wonder why we need a batch file instead of calling rimraf directly from the registry. Here's why:
- npx handling: The batch file allows npx to properly handle installation prompts
- Window visibility: The command prompt shows progress and errors
- Pause on completion: Users can see the result before the window closes
- Easy debugging: Batch files are simple to edit and test
Conclusion
With this simple PowerShell script, you've supercharged Windows File Explorer with the power of rimraf. No more struggling with deeply nested folders or path length limitations. Just right-click and delete!
The script demonstrates several useful Windows automation techniques:
- Administrator privilege checking
- Registry manipulation via
reg.exe - Context menu customization
- Batch file generation
Feel free to adapt this approach for other command-line tools you'd like to integrate into your Windows workflow.
Happy deleting! 🗑️