Using PowerShell to Delete Files and Folders

Author avatar - Adam Bertram

Adam Bertram


Simple as a deletion of a file or folder is, it is important to understand how to do this operation in PowerShell. Many business processes rely on a combination of file operations and inevitably need to delete process files, temporary files, or clean up sensitive data. In this article, we explore how to delete files and folders in PowerShell 7, and what are limitations exist.

Delete files and folders using PowerShell

Following the typical PowerShell noun-verb convention, to delete either a file or folder, you will use the Remove-Item cmdlet. To remove a file, simply pass the filename to be removed via the -Path parameter, or as the first argument. With a successful operation, you will not receive an output message. Therefore, for this example, we are passing the -Verbose parameter to show that the file we intend to remove has been removed.


Remove-Item -Path File2.txt -Verbose
Removing a File using the Remove-Item cmdlet.

To remove a directory, the same command is used, but with the -Recurse parameter. The -Recurse parameter is necessary if the directory is not empty, otherwise Remove-Item will prompt you for confirmation on every item contained in the directory.

Remove-Item -Path C:\\Articles\\Folder1 -Recurse -Verbose


Image #1 Expand
Removing a non-empty directory.

What about using PowerShell in Linux? The same commands apply as you can see in the below example where we delete both a non-empty folder and a file.

Remove-Item -Path file1.txt -Verbose
Remove-Item -Path ~/articles/Folder1 -Recurse -Verbose

Remove-Item can work on long file names in both Linux and Windows. Traditionally, Windows tools were limited to 255 characters but in PowerShell 7 this limitation does not typically appear. If you do find yourself having trouble removing a file via a long path, you can use the special Windows API for Unicode path names up to 32k characters. This format consists of \\?\ preceding a typical path like this example, \\?\\C:\\Articles\\File3.txt.


Special cases for removing files and folders using PowerShell

There are a number of special cases that you may encounter when attempting to remove files and folders. Depending on the attributes a given file or folder may have, you may not be able to enumerate the file or folder, and need to tell Remove-Item to process the input differently.

Removing a hidden file

Files that are marked as hidden, may not show in Get-ChildItem output. Using the -Hidden flag allows you to locate these files. When attempting to remove these files, you will need to -Force the removal.

Remove-Item -Path File1.txt -Verbose -Force
Removing a hidden file.

With a file marked as read-only, as denoted by the r in the Mode display, as seen in the return by Get-ChildItem, we need to tell Remove-Item to -Force the removal. If we do not, you see the error message as shown below.

Remove-Item -Path File0.txt -Verbose -Force
Removing a hidden file.

Unblocking a file using Remove-Item

When a file is downloaded from the internet, Windows adds a Zone Identifier in the alternate data stream. This data stream will indicate that the file should be blocked from running until a manual action to unblock the file is taken.


Showing that a file is blocked after being downloaded from the internet.

As we can see below, there is an alternate stream of Zone.Identifier on this file that is causing the file to be shown as blocked.

Get-Item -Path File2.txt -Stream *
Displaying all streams associated with a file.

Interestingly enough, you can use Remove-Item to remove this alternate data stream which has the same effect as checking the Unblock checkbox. To do this, simply use the following code.

Remove-Item -Path File2.txt -Stream Zone.Identifier
Displaying all streams associated with a file.

As you can see, this does not actually remove the file but just the alternate data stream itself. This can be very useful as alternate data streams can contain different content, even entire executables, so this provides a convenient way to remove that data from a file.

This is a Windows-only concept, so this type of command will not work in PowerShell 7 on Linux.


The Remove-Item command is very useful on both Windows and Linux. Able to delete files, folders, and alternate data streams, this command is very useful for managing files and folders within scripts and on the command line. With the ability to easily handle read-only, hidden, and files and folders contained within long paths, Remove-Item is flexible and easy to use!

And if you would like to move files using PowerShell, check out How Can I Move a File or Folder from the Command Line Using Windows PowerShell? on Petri. We have also a separate guide on How to Create Files and Folders with PowerShell.

Article saved!

Access saved content from your profile page. View Saved