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.

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

-Path PATHSpecify the file or folder or path to be deleted
-ForceForce the delete operation even the target is hidden, read-only, etc.
-IncludeDelete only specified names
-RecurseDelete recursively
-ConfirmConfirm for delete

Delete File

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

PS> Remove-Item Test.txt

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 provide the -Path parameter where we can also specify the 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 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.

READ  "ipconfig /registerdns" - Register Hostname To Active Directory (AD)

In the following example the “*.txt” files will not 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