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.
Remove-Item -Path File2.txt -Verbose
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
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-Itemcan 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,
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.
Remove-Item -Path File1.txt -Verbose -Force
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
-Force the removal. If we do not, you see the error message as shown below.
Remove-Item -Path File0.txt -Verbose -Force
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.
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 *
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
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.
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!