Using PowerShell to Delete Files and Folders

PowerShell

In this article, we explore how to delete files and folders in PowerShell, and what limitations exist. 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.

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.

Delete a single file using PowerShell 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

PowerShell delete file using Remove-Item
Delete a file using the PowerShell Remove-Item cmdlet

Delete a single folder

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

Delete a folder using PowerShell Remove-Item cmdlet
Delete a folder using the PowerShell Remove-Item cmdlet

Delete multiple files using PowerShell

To delete multiple files in a specified location, you just need to add /*.* to the end of the file path. For example, the following command deletes all files in Folder2 but not the parent folder.

Remove-Item -Path C:\Articles\Folder2\*.*

Delete multiple files but exclude folders using Remote-Item and Where-Object

The above command also deletes any files in the parent folder, if they exist. To exclude folders from the delete operation, use the following command:

Remove-Item C:\Articles\Folder2\*.* | Where { ! $_.PSIsContainer }

Check folder contents before deleting files using PowerShell Get-ChildItem

Another way to achieve the same thing is to check the contents of the specified path first using Get-ChildItem and then passing the results to Remove-Item.

Get-ChildItem -Path C:\Articles\Folder2 -File -Recurse | Remove-Item -Verbose

Delete files with a specific extension using PowerShell

You can also delete files with a specific file extension. For example, here’s a command you can use to delete all files in a given path with the extension .mp3:

Remove-Item -Path C:\Articles\Folder2\* -Include *.mp3

Delete a file and folder in Linux using PowerShell

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 deleting 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.

Delete a hidden file using PowerShell Remove-Item

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

Working with hidden items
Remove 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

Remove a read-only item
Remove a read-only item

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
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
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
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.

Conclusion

The Remove-Item command is useful on both Windows and Linux. The ability to delete files, folders, and alternate data streams, is useful for managing files and folders within scripts and on the command line. You can 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.