Add rimraf to Windows Context Menu: Delete node_modules Like a Pro

Dia Loghmari

Dia Loghmari / January 13, 2026

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:

  1. Downloads a delete icon and stores it in %USERPROFILE%\.actions\rimraf
  2. Creates a batch file to execute rimraf
  3. Registers context menu entries for files, folders, and folder backgrounds

Create a file named setup-rimraf-context-menu.ps1:

# PowerShell script to create a Windows context menu for rimraf
# This script must be run as Administrator

# Check if running as Administrator
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
$isAdmin = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

if (-not $isAdmin) {
Write-Host "This script must be run as Administrator!" -ForegroundColor Red
Write-Host "Please right-click the script and select 'Run as Administrator'" -ForegroundColor Yellow
pause
exit
}

# Step 1: Download icon and create directory structure
$actionsDir = "$env:USERPROFILE\.actions\rimraf"
$iconPath = "$actionsDir\delete_icon.ico"

Write-Host "Creating directory structure..." -ForegroundColor Cyan
if (-not (Test-Path $actionsDir)) {
New-Item -ItemType Directory -Path $actionsDir -Force | Out-Null
}

Write-Host "Downloading icon..." -ForegroundColor Cyan
$iconUrl = "https://icon-icons.com/download-file?file=https%3A%2F%2Fimages.icon-icons.com%2F3628%2FICO%2F64%2Fdelete_icon_227356.ico&id=227356&pack_or_individual=pack"
try {
Invoke-WebRequest -Uri $iconUrl -OutFile $iconPath -UseBasicParsing
Write-Host "Icon downloaded successfully to: $iconPath" -ForegroundColor Green
} catch {
Write-Host "Failed to download icon: $_" -ForegroundColor Red
Write-Host "Continuing without icon..." -ForegroundColor Yellow
$iconPath = $null
}

# Step 2: Create batch file for rimraf
Write-Host "Creating batch file..." -ForegroundColor Cyan

$batchPath = "$actionsDir\rimraf.bat"
$batchContent = @'
@echo off
npx rimraf@6 %1
pause
'@

Set-Content -Path $batchPath -Value $batchContent
Write-Host "Batch file created at: $batchPath" -ForegroundColor Green

# Registry paths for files and folders (using Registry:: provider directly)
$regPathFile = "Registry::HKEY_CLASSES_ROOT\*\shell\rimraf"
$regPathFolder = "Registry::HKEY_CLASSES_ROOT\Directory\shell\rimraf"
$regPathBackground = "Registry::HKEY_CLASSES_ROOT\Directory\Background\shell\rimraf"

# Function to create registry entry using reg.exe
function Add-ContextMenuEntry {
param(
[string]$RegPath,
[string]$IconPath,
[string]$BatchPath,
[string]$Placeholder = "%1"
)

try {
Write-Host " Creating registry key using reg.exe..." -ForegroundColor Gray

# Convert Registry:: path to reg.exe format (HKCR)
$regExePath = $RegPath -replace "Registry::HKEY_CLASSES_ROOT", "HKCR"

# Create main key using reg.exe
$result = reg add "$regExePath" /ve /d "Delete with rimraf" /f 2>&1
if ($LASTEXITCODE -ne 0) {
throw "Failed to create registry key: $result"
}

if ($IconPath -and (Test-Path $IconPath)) {
Write-Host " Setting icon..." -ForegroundColor Gray
$result = reg add "$regExePath" /v "Icon" /d "$IconPath" /f 2>&1
if ($LASTEXITCODE -ne 0) {
throw "Failed to set icon: $result"
}
}

# Create command subkey
$command = "`"$BatchPath`" `"$Placeholder`""
Write-Host " Setting command: $command" -ForegroundColor Gray
$result = reg add "$regExePath\command" /ve /d "$command" /f 2>&1
if ($LASTEXITCODE -ne 0) {
throw "Failed to set command: $result"
}

Write-Host " Success!" -ForegroundColor Green
} catch {
Write-Host " Failed: $_" -ForegroundColor Red
throw
}
}

# Add context menu for files
Write-Host "Adding context menu for files..." -ForegroundColor Cyan
Add-ContextMenuEntry -RegPath $regPathFile -IconPath $iconPath -BatchPath $batchPath -Placeholder "%1"

# Add context menu for folders
Write-Host "Adding context menu for folders..." -ForegroundColor Cyan
Add-ContextMenuEntry -RegPath $regPathFolder -IconPath $iconPath -BatchPath $batchPath -Placeholder "%1"

# Add context menu for folder background (right-click in empty space)
Write-Host "Adding context menu for folder background..." -ForegroundColor Cyan
Add-ContextMenuEntry -RegPath $regPathBackground -IconPath $iconPath -BatchPath $batchPath -Placeholder "%V"

Write-Host "`nContext menu setup complete!" -ForegroundColor Green
Write-Host "Right-click on any file or folder to see the 'Delete with rimraf' option." -ForegroundColor Cyan
Write-Host "`nNote: You may need to restart Windows Explorer for changes to take effect." -ForegroundColor Yellow
Write-Host "To restart Explorer, run: taskkill /f /im explorer.exe & start explorer.exe" -ForegroundColor Yellow

pause
# PowerShell script to create a Windows context menu for rimraf
# This script must be run as Administrator

# Check if running as Administrator
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
$isAdmin = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

if (-not $isAdmin) {
Write-Host "This script must be run as Administrator!" -ForegroundColor Red
Write-Host "Please right-click the script and select 'Run as Administrator'" -ForegroundColor Yellow
pause
exit
}

# Step 1: Download icon and create directory structure
$actionsDir = "$env:USERPROFILE\.actions\rimraf"
$iconPath = "$actionsDir\delete_icon.ico"

Write-Host "Creating directory structure..." -ForegroundColor Cyan
if (-not (Test-Path $actionsDir)) {
New-Item -ItemType Directory -Path $actionsDir -Force | Out-Null
}

Write-Host "Downloading icon..." -ForegroundColor Cyan
$iconUrl = "https://icon-icons.com/download-file?file=https%3A%2F%2Fimages.icon-icons.com%2F3628%2FICO%2F64%2Fdelete_icon_227356.ico&id=227356&pack_or_individual=pack"
try {
Invoke-WebRequest -Uri $iconUrl -OutFile $iconPath -UseBasicParsing
Write-Host "Icon downloaded successfully to: $iconPath" -ForegroundColor Green
} catch {
Write-Host "Failed to download icon: $_" -ForegroundColor Red
Write-Host "Continuing without icon..." -ForegroundColor Yellow
$iconPath = $null
}

# Step 2: Create batch file for rimraf
Write-Host "Creating batch file..." -ForegroundColor Cyan

$batchPath = "$actionsDir\rimraf.bat"
$batchContent = @'
@echo off
npx rimraf@6 %1
pause
'@

Set-Content -Path $batchPath -Value $batchContent
Write-Host "Batch file created at: $batchPath" -ForegroundColor Green

# Registry paths for files and folders (using Registry:: provider directly)
$regPathFile = "Registry::HKEY_CLASSES_ROOT\*\shell\rimraf"
$regPathFolder = "Registry::HKEY_CLASSES_ROOT\Directory\shell\rimraf"
$regPathBackground = "Registry::HKEY_CLASSES_ROOT\Directory\Background\shell\rimraf"

# Function to create registry entry using reg.exe
function Add-ContextMenuEntry {
param(
[string]$RegPath,
[string]$IconPath,
[string]$BatchPath,
[string]$Placeholder = "%1"
)

try {
Write-Host " Creating registry key using reg.exe..." -ForegroundColor Gray

# Convert Registry:: path to reg.exe format (HKCR)
$regExePath = $RegPath -replace "Registry::HKEY_CLASSES_ROOT", "HKCR"

# Create main key using reg.exe
$result = reg add "$regExePath" /ve /d "Delete with rimraf" /f 2>&1
if ($LASTEXITCODE -ne 0) {
throw "Failed to create registry key: $result"
}

if ($IconPath -and (Test-Path $IconPath)) {
Write-Host " Setting icon..." -ForegroundColor Gray
$result = reg add "$regExePath" /v "Icon" /d "$IconPath" /f 2>&1
if ($LASTEXITCODE -ne 0) {
throw "Failed to set icon: $result"
}
}

# Create command subkey
$command = "`"$BatchPath`" `"$Placeholder`""
Write-Host " Setting command: $command" -ForegroundColor Gray
$result = reg add "$regExePath\command" /ve /d "$command" /f 2>&1
if ($LASTEXITCODE -ne 0) {
throw "Failed to set command: $result"
}

Write-Host " Success!" -ForegroundColor Green
} catch {
Write-Host " Failed: $_" -ForegroundColor Red
throw
}
}

# Add context menu for files
Write-Host "Adding context menu for files..." -ForegroundColor Cyan
Add-ContextMenuEntry -RegPath $regPathFile -IconPath $iconPath -BatchPath $batchPath -Placeholder "%1"

# Add context menu for folders
Write-Host "Adding context menu for folders..." -ForegroundColor Cyan
Add-ContextMenuEntry -RegPath $regPathFolder -IconPath $iconPath -BatchPath $batchPath -Placeholder "%1"

# Add context menu for folder background (right-click in empty space)
Write-Host "Adding context menu for folder background..." -ForegroundColor Cyan
Add-ContextMenuEntry -RegPath $regPathBackground -IconPath $iconPath -BatchPath $batchPath -Placeholder "%V"

Write-Host "`nContext menu setup complete!" -ForegroundColor Green
Write-Host "Right-click on any file or folder to see the 'Delete with rimraf' option." -ForegroundColor Cyan
Write-Host "`nNote: You may need to restart Windows Explorer for changes to take effect." -ForegroundColor Yellow
Write-Host "To restart Explorer, run: taskkill /f /im explorer.exe & start explorer.exe" -ForegroundColor Yellow

pause

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 icon
  • rimraf.bat - The batch file that executes rimraf

3. Batch File

The batch file is simple but effective:

@echo off
npx rimraf@6 %1
pause
@echo off
npx rimraf@6 %1
pause

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

  1. Save the script as setup-rimraf-context-menu.ps1

  2. Right-click the script and select "Run with PowerShell" or "Run as Administrator"

  3. The script will:

    • Create the .actions\rimraf directory
    • Download the delete icon
    • Create the batch file
    • Register context menu entries
  4. If needed, restart Windows Explorer:

    taskkill /f /im explorer.exe & start explorer.exe
    taskkill /f /im explorer.exe & start explorer.exe

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:

$result = reg add "$regExePath" /ve /d "Your Custom Text" /f 2>&1
$result = reg add "$regExePath" /ve /d "Your Custom Text" /f 2>&1

Use a Different Tool

Replace npx rimraf@6 in the batch file with any command:

@echo off
your-command %1
pause
@echo off
your-command %1
pause

Why This Approach?

You might wonder why we need a batch file instead of calling rimraf directly from the registry. Here's why:

  1. npx handling: The batch file allows npx to properly handle installation prompts
  2. Window visibility: The command prompt shows progress and errors
  3. Pause on completion: Users can see the result before the window closes
  4. 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! 🗑️