Using PowerShell to Delete Files and Folders

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.

Simple Deletion of Files and Folders

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.

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.


 

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

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.

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.

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.

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.

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

Related Topics:

  • PowerShell
  • BECOME A PETRI MEMBER:

    Don't have a login but want to join the conversation? Sign up for a Petri Account

    Register
    Entrepreneur, hustler, husband, dad, Automator, content producer, published author, Microsoft MVP, DevOps pro and passionate problem-solver.