How To Delete Files In PowerShell?

PowerShell provides the Remove-Item which can be used to remove or delete files and folders. Remove-Item can be used in different ways. By using the Remove-Item cmdlet we can delete files and folders from the PowerShell command line by using the absolute name, recursively, according to the extension, etc.

Remove-Item Syntax

Remove-Item syntax has very simple syntax where parameters and the file or folder name will be provided like below.

Remove-Item PARAMETER PATH
  • PARAMETER is used to set different options for the delete operation.
  • PATH is used to specify the file and folder name or path to be deleted.

Remove-Item Parameters

The Remove-Item cmdlet provides the following parameters where all of them are optional.

PARAMETER DESCRIPTION
-Path PATH Specify the file or folder or path to be deleted
-Force Force the delete operation even if the target is hidden, read-only, etc.
-Include Delete only specified names
-Recurse Delete recursively
-Confirm Confirm for delete

Delete File

We can delete a file using the Remove-Item cmdlet and provide the file as a parameter like below. In the following example, we will delete the file named Test.txt .

PS> Remove-Item Test.txt

Delete File with Absolute Path

Alternatively, we can specify the file absolute path by providing the full path of the file like below. This will prevent accidental delete or will work for all different working paths. In the following example, we will delete the Test.txt which is located under C:\Users\İsmail\File.txt .

PS> Remove-Item "C:\Users\İsmail\Test.txt"

The Remove-Item cmdlet also provides the -Path parameter where we can also specify the file name we want to delete in a more structured way. This will also prevent errors and will be more readable.

PS> Remove-Item -Path Test.txt

We can also provide the complete or full or absolute path with the -Path parameter like below.

PS> Remove-Item -Path "C:\Users\İsmail\Test.txt"

Delete According To File Extension

We can also use file extensions in order to specify the files we want to delete with the Remove-Item cmdlet. In the following example, we will delete the files with the extension *.txt which means all text files will be deleted. This will delete all text files in the current working directory.

PS> Remove-Item *.txt

We can also specify the full or complete or absolute path we want to delete which will be less error-prone. The text files located under the C:\Users\İsmail will be deleted.

PS> Remove-Item "C:\Users\İsmail\*.txt"

We can also use the -Include parameter in order to specify the file extension in a more structured way. In the following example, we will delete all *.txt files.

PS> Remove-Item -Include "*.txt" "C:\Users\İsmail\" 

Delete Files with Any Extension

The Remove-Item can be used to delete files with any extension. If a file does not have an extension it is not deleted with the following command.

PS> Remove-Item C:\Backup\*.*

Delete Documents (Word) Files

By using the following commands we can delete the word documents by using the *.doc extension.

PS> Remove-Item C:\Backup\*.doc

Delete Recursively

We may remove files recursively under the specified folder or path. This will delete or remove all files in the specified path and under the sub-folders. The Remove-Item will use the -Recursive parameter like below. The following command will remove all text files under the “C:\User\İsmail\” and its sub-folders.

PS> Remove-Item -Recursive -Include "*.txt" "C:\Users\İsmail\" 

Delete Hidden and Read-Only Files with -Force Option

The -Force option is generally used to delete files that have attributes like Hidden or Read-Only. By default, these files are not deleted unless the -Force option is specified. In the following example, we will delete all files as well as hidden and read-only files under the “C:\Backup” folder.

PS> Remove-Item -Recursive -Force "C:\Backup"

Confirm Before Delete a File or Directory

By default deleted files and directories will no confirmed for the removal. But if you want to confirm before delete operation you can use the -Confirm option like below. This will ask for confirmation for every files and directory which will be deleted.

PS> Remove-Item -Recursive -Confirm -Force "C:\Backup"

Delete Multiple Files

The Remove-Item command supports removal of multiple files and directories and specifying them in a single command.

PS> Remove-Item -Recursive "C:\Backup" "C:\Test" "D:\Images"

Exclude Specific File Types or Extensions

The specific file type of extension can be excluded and the given path will not delete even other files and folders are deleted. The -Exclude option is used with the file name, extension, or file name pattern to exclude from deletion.

In the following example, the “*.txt” files will not be deleted.

PS> Remove-Item -Exclude *.txt  -Recursive "C:\Backup"

In the following example file names starting with “Important*” will not deleted.

PS> Remove-Item -Exclude "Important*"  -Recursive "C:\Backup"

Leave a Comment