Troubleshooting File Deletion Issues in PowerShell: Unable to Delete Opened Files
Use this when PowerShell cannot delete a file because another process holds it open.
Quick Read
- Symptom: Use this when PowerShell cannot delete a file because another process holds it open.
- Check first: Confirm OS build, domain or workgroup state, local admin rights, and whether the host is managed by GPO, Intune, or another baseline.
- Risk: Changes system state
Symptoms
PowerShell is unable to delete a file that is currently opened by a process.
Environment
Windows operating systems with PowerShell installed.
Most Likely Causes
Files that are opened by a process are locked, preventing deletion until the file is closed by the application that is using it.
What to Check First
- Confirm OS build, domain or workgroup state, local admin rights, and whether the host is managed by GPO, Intune, or another baseline.
- Collect the exact error code, Event Viewer entries, and the command or UI action that triggers the failure.
- Check whether the issue follows the user profile, machine, network, or application package.
Fix Steps
- Identify the Process Locking the File
Use the Resource Monitor to find which process is using the file you want to delete.
Example pattern only. Adjust for your environment before running.
Open Resource Monitor by typing 'resmon' in the Run dialog (Win + R). Navigate to the 'CPU' tab. In the 'Associated Handles' section, enter the name of the file in the search box.
- Terminate the Process Using the File
Once you have identified the process, you can terminate it to release the file lock.
Example pattern only. Adjust for your environment before running.
Open Task Manager by pressing Ctrl + Shift + Esc. Find the process identified in the Resource Monitor. Right-click on the process and select 'End Task'.
- Delete the File Using PowerShell
After terminating the process, attempt to delete the file again using PowerShell.
Changes system state: review before running
Open PowerShell as Administrator. Use the command: Remove-Item 'C:\path\to\your\file.txt' -Force
- Verify File Deletion
Check if the file has been successfully deleted.
Example pattern only. Adjust for your environment before running.
Use the command: Test-Path 'C:\path\to\your\file.txt' If the output is 'False', the file has been deleted.
Validation
- The failing Windows action completes after reboot or service restart if the remediation requires one.
- Event Viewer stops logging the same error ID for the same component during a retest.
- The fix works for the affected standard user context, not only for an elevated administrator session.
Logs to Check
- Event Viewer: System, Application, Setup, WindowsUpdateClient, TerminalServices, or PowerShell logs as relevant.
- CBS.log, DISM.log, or WindowsUpdate.log when servicing or feature installation is involved.
- Security, RDP, or application-specific logs for authentication and session failures.
Rollback and Escalation
- Record the original registry, service, feature, policy, or firewall value before changing it.
- Undo temporary local policy, firewall, or service changes after validation.
- Use a restore point, VM snapshot, or exported configuration when changing servicing, boot, or security settings.
Escalate When
- Escalate if the same error persists after rollback and a clean retry from the original failing path.
- Escalate if logs show authorization, data loss, certificate, replication, or production availability risk outside the local service owner scope.
Edge Cases
- If the process cannot be terminated due to system restrictions, consider restarting the system to release the file lock.
- If the file is part of a critical system operation, ensure that terminating the process will not affect system stability.
Notes from the Field
- If the machine is domain-managed, local fixes can be overwritten. Check the winning GPO or MDM policy before repeating the same change.
- Prefer read-only collection first on Windows incidents because many repair commands change component store, services, or user profile state.