How to forcefully delete a folder in Windows using PowerShell?

Force deleting a folder in Windows refers to deleting a folder that cannot be removed through normal delete operations, usually because the folder is in use by a process or open in an application. When attempting to delete a folder in use, Windows will typically return an error stating “The folder is in use and cannot be deleted.” Force deleting overrides this protection and deletes the folder regardless of its status.

Force deleting may be necessary when a folder contains corrupt files preventing normal access, when a process has crashed and locked the folder, or when you simply need to completely remove a folder without closing any applications using it first. PowerShell provides a powerful command-line method to force delete folders in Windows without extra software.

This guide will walk through using the PowerShell Remove-Item cmdlet to forcefully delete a protected folder. We’ll cover disabling the Recycle Bin, removing the folder, and re-enabling the Recycle Bin to restore normal delete operations.

Prerequisites

Before you can use PowerShell to forcefully delete a folder in Windows, you need to make sure you have PowerShell installed and updated. PowerShell comes pre-installed on Windows 10 and Windows 11, but you may need to update to the latest version.

To check your PowerShell version, open a PowerShell window and type $PSVersionTable. This will display information about the installed version.

If you are not running PowerShell 5.0 or later, you will need to update it. You can download the latest version from the Microsoft Docs.

Once PowerShell is installed and updated, you will be able to use the commands required to forcefully delete folders in Windows.

Identify Folder to Delete

The first step is to identify the folder path of the folder you want to delete. This path allows you to specify the exact folder location that will be deleted.

To find the folder path in Windows 10:

  1. Open File Explorer and navigate to the folder you want to delete.
  2. Right click on the folder and select “Properties”.
  3. In the Properties window, switch to the “Security” tab.
  4. Look under “Object name” to find the folder path.

For example, the path may look like: C:\Users\Name\FolderToDelete

Make note of this full folder path, as you will need to enter it in the PowerShell command.

Open PowerShell

To forcefully delete a folder in Windows, you will need to open PowerShell with administrator privileges. This allows PowerShell to bypass the usual permissions and access protected folders.

Here are the steps to open PowerShell as an administrator on Windows 10:

  1. Click the Start menu and type “PowerShell”.
  2. Right-click on “Windows PowerShell” and select “Run as administrator”.
  3. If prompted by User Account Control, click Yes to allow PowerShell to make changes to your device.

This will open a PowerShell window with admin access, indicated by “Administrator” in the title bar. You can now run commands that require elevated permissions like force deleting folders.

Alternatively, you can also launch PowerShell as admin from the Windows Power User menu. Just right-click the Start button or press Windows+X and select “Windows PowerShell (Admin)” [1].

Disable Recycle Bin

The Recycle Bin must be disabled in order to force delete a folder in Windows using PowerShell. When enabled, the Recycle Bin acts as a temporary holding place for deleted files, preventing them from being permanently erased until the Recycle Bin is emptied. Disabling the Recycle Bin bypasses this protection and allows files and folders to be immediately and irrevocably deleted.

To disable the Recycle Bin, the following PowerShell command can be used:

Remove-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Explorer" -Name "NoRecycleFiles"

This edits the registry to disable the Recycle Bin 1. Once run, any subsequent delete operations will permanently destroy files rather than moving them to the Recycle Bin.

It’s important to note that disabling the Recycle Bin affects the entire system, not just the current PowerShell session. Users will no longer have a safety net when deleting files. Therefore, caution is advised when force deleting system folders in this manner.

Force Delete Folder

To force delete the folder in PowerShell, use the Remove-Item cmdlet with the -Force parameter. Here is the basic syntax:

Remove-Item -Path C:\FolderToDelete -Recurse -Force

The -Path parameter specifies the path to the folder you want to delete. Make sure to use the full path. The -Recurse switch deletes all contents of the folder recursively. And the -Force parameter forces the deletion, overriding any restrictions or permissions.

For example, to force delete a folder named TestFolder on the C drive, the command would be:

Remove-Item -Path C:\TestFolder -Recurse -Force

This will delete TestFolder and all its contents, without prompting for confirmation or checking permissions. Be very careful when using -Force as it cannot be undone.

According to Microsoft’s Remove-Item documentation, the -Force parameter overrides restrictions that prevent the deletion of read-only files, hidden files, and system files.

Confirm Deletion

Once the folder has been deleted, it’s important to verify that it no longer exists. This can be done by navigating to the location where the folder was and confirming it is no longer there.

For example, if the deleted folder was located at C:\Users\Username\Documents\OldFolder, you can navigate there in File Explorer and confirm OldFolder is gone. Alternatively, you can use the PowerShell command Get-ChildItem to list the contents of that directory and pipe it to Select-String to search for the deleted folder name:

Get-ChildItem C:\Users\Username\Documents | Select-String OldFolder

If the folder was successfully deleted, this will not return any results. You can also try recreating the exact path to the deleted folder and verify you get an error that the path does not exist:

Test-Path C:\Users\Username\Documents\OldFolder

The Test-Path cmdlet will return $False if the path no longer exists. These simple checks confirm the folder was permanently deleted as intended.

Re-enable Recycle Bin

After force deleting the folder, you will likely want to turn the Recycle Bin back on. The Recycle Bin provides a safety net against accidental deletions. Here are the steps to re-enable it:

First, open PowerShell again as administrator. Then run the command:

Enable-ADOptionalFeature 'Recycle Bin Feature' -Scope ForestOrConfigurationSet -Target 'yourdomain.com'

Replace ‘yourdomain.com’ with your actual domain name. This will turn the Active Directory Recycle Bin back on across your entire domain controller forest [1].

You can confirm it is enabled again by running Get-ADOptionalFeature -Filter 'Recycle Bin Feature' which should return ‘Enabled’ [2].

Now deleted folders will go to the Recycle Bin instead of being permanently deleted. This provides a safety net for restores in case you or users accidentally delete something important.

Alternative Methods

There are a few other methods you can use to force delete a folder in Windows besides using PowerShell:

Force delete from Safe Mode – You can boot into Windows Safe Mode and then delete the folder. Safe Mode loads only the essential drivers and services, so it can sometimes bypass issues that prevent deletion.

Shift + Delete – Select the folder and use the Shift + Delete keyboard shortcut to force delete it. This bypasses the Recycle Bin.

Third party software – Utilities like Unlocker or Recuva can sometimes force delete locked or in-use folders.

The PowerShell method provides more control and options compared to these alternatives. But in certain cases, one of these other methods may be quicker or more effective.

Troubleshooting

Here are some solutions for common errors you may encounter when trying to force delete a folder in PowerShell:

Access Denied

If you get an “Access Denied” error, it’s likely because you don’t have permissions to delete the folder. Try running PowerShell as an Administrator to elevate your permissions. You can also take ownership of the folder first using Takeown /f FolderPath /r /d y and then attempting the deletion again.

Sources:

https://stackoverflow.com/questions/38141528/cannot-remove-item-the-directory-is-not-empty

https://recoverit.wondershare.com/deleted-file-recovery/powershell-to-force-delete-file-and-folder.html

Folder in Use

If you get a “Folder in Use” error, it means one or more files in the folder are currently open or locked by another process. Close any open files and try again. If that doesn’t work, you can use a tool like Handle to find and kill the locking process before deleting.

Sources:

https://serverfault.com/questions/199921/force-remove-files-and-directories-in-powershell-fails-sometimes-but-not-always

Not Empty

A “Directory Not Empty” error means there are still files or subfolders present. Make sure to use the -Recurse parameter to delete the entire folder hierarchy. You can also delete the contents first with Get-ChildItem | Remove-Item and then delete the parent folder.

Sources:

https://stackoverflow.com/questions/38141528/cannot-remove-item-the-directory-is-not-empty