What This Covers

You have a file or folder that won’t delete — Access Denied, locked by the system, or held open by a running process. This covers three methods to force-delete it: Command Prompt, PowerShell, and scheduling deletion at next boot using Task Scheduler with SYSTEM rights.

Before You Start

  • Run everything as Administrator — right-click CMD or PowerShell → Run as administrator
  • Set the path variable at the top of each method — everything else uses it
  • This is irreversible. Double-check the path before you run anything
  • These methods bypass permission locks — use only on files you own or manage

Steps

Method 1: Command Prompt

set TARGET=C:\path\to\locked-folder

takeown /F %TARGET% /R /A
icacls %TARGET% /grant Administrators:F /T
rd /S /Q %TARGET%

For a single file, replace the last line with:

del /F /Q %TARGET%

What these do:

  • set TARGET= — define the path once, reuse everywhere
  • takeown — reclaims ownership from SYSTEM or TrustedInstaller
  • icacls — grants your account full control
  • rd /S /Q — deletes folder and all contents, no prompt
  • del /F /Q — force deletes a file, no prompt

[i] INFO: rd /S /Q and del /F /Q only work in Command Prompt. In PowerShell, rd is an alias for Remove-Item and ignores these switches entirely.


Method 2: PowerShell

$Target = "C:\path\to\locked-folder"

takeown /F $Target /R /A
icacls $Target /grant Administrators:F /T
Remove-Item -Path $Target -Recurse -Force

For a single file, replace the last line with:

Remove-Item -Path $Target -Force

[!] WARNING: -Recurse -Force deletes everything inside the folder without confirmation. Verify $Target before running.

[i] INFO: takeown and icacls are native Windows executables — they work identically from CMD or PowerShell.


Method 3: Schedule Deletion at Next Boot via Task Scheduler

Use this when a file or folder is actively locked by a running process and cannot be deleted while Windows is up. Task Scheduler runs as SYSTEM at boot — before most services and locks exist — giving it rights that even an Administrator session doesn’t always have.

Why Task Scheduler over other reboot methods:

  • Runs as SYSTEM — highest local rights, no UAC
  • Fires at boot before logon — most locks already released
  • Handles recursive folder deletion natively
  • One-shot — fires once and is gone, no manual cleanup needed

Command Prompt (elevated):

set TARGET=C:\path\to\locked-folder

schtasks /create /tn "DeleteOnBoot" /tr "powershell.exe -NoProfile -WindowStyle Hidden -Command \"Remove-Item -Path '%TARGET%' -Recurse -Force\"" /sc once /st 00:00 /sd 01/01/2000 /ru SYSTEM /rl HIGHEST /f

PowerShell (elevated):

$Target = "C:\path\to\locked-folder"

$Action   = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -WindowStyle Hidden -Command `"Remove-Item -Path '$Target' -Recurse -Force`""
$Trigger  = New-ScheduledTaskTrigger -Once -At "00:00"
$Settings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Minutes 5) -DeleteExpiredTaskAfter (New-TimeSpan -Seconds 0)

Register-ScheduledTask -TaskName "DeleteOnBoot" -Action $Action -Trigger $Trigger -Settings $Settings -RunLevel Highest -User "SYSTEM" -Force

-DeleteExpiredTaskAfter (New-TimeSpan -Seconds 0) tells Task Scheduler to remove the task immediately after it fires — nothing left behind.

[i] INFO: /sd 01/01/2000 sets the start date in the past so Task Scheduler treats the task as already overdue and runs it at the very next boot.

[!] WARNING: Must be run from an elevated CMD or PowerShell session. Running without Administrator rights will return Access Denied.

Reboot, then verify:

$Target = "C:\path\to\locked-folder"
Test-Path $Target

Returns False — done.


If All Else Fails — Robocopy Mirror Trick

For stubborn folders that won’t respond to any delete command:

set TARGET=C:\path\to\stubborn-folder

mkdir C:\empty
robocopy C:\empty %TARGET% /MIR
rd /S /Q %TARGET%
rmdir C:\empty

/MIR mirrors an empty folder over the target, wiping all contents — which sidesteps the lock. Then rd removes the now-empty shell.

Verify

After any deletion, confirm the path is gone:

$Target = "C:\path\to\locked-folder"
Test-Path $Target

Returns False — you’re done.

Notes

  • CMD vs PowerShell syntax is not interchangeable. $Variable is PowerShell. set VARIABLE= is CMD. Backtick ` is PowerShell line continuation. ^ is CMD. Mixing them will fail
  • C:\Windows.old is protected by TrustedInstaller — takeown + icacls is always required first
  • “The process cannot access the file because it is being used by another process” — that’s a file lock. Use Method 3
  • PendingFileRenameOperations registry key is file-only — it cannot delete folders recursively. Task Scheduler is the correct tool for locked folders at boot
  • Prefer Method 2 (PowerShell) if you’re already in a PowerShell session — error output is cleaner when something fails

[i] Tested on: Windows 11 24H2, Windows Server 2022 [i] Requires: Local Administrator or Domain Admin [!] WARNING: All three methods are irreversible. No recycle bin, no undo.


Questions or feedback? Reach out on LinkedIn or leave a comment below.

🤝 Connect with Me

Found this useful? I write about PowerShell, Windows infrastructure, and enterprise automation.