≡ Menu

Powershell: How To Delete Files

This post explains how to delete files using Powershell command ‘Remove-Item’.

Delete file from PowerShell

We can delete a file using remove-item command as below. Open powershell prompt and execute the command.

remove-item file-path

Example:

PS C:\> Remove-Item C:\test\testFile.txt
PS C:\>

Delete multiple files

We can delete as many files as we want with single remove-item command. We just need to add the file names separated by comma. See example below.

PS C:\> Remove-Item C:\dir1\file1, C:\dir1\file2, C:\dir2\file3
PS C:\>

Remove files with wild characters

Remove-item command accepts wildcards too, using which we can delete files in bulk.

Remove-Item *.csv

The above command deletes all files with csv extension.
If the file does not exist, the command throws error as shown below.

PS C:\> Remove-Item C:\dir1\FileThatDoesNotExist.txt
Remove-Item : Cannot find path 'C:\dir1\FileThatDoesNotExist.txt' because it does not exist.
At line:1 char:1
+ Remove-Item C:\dir1\FileThatDoesNotExist.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\dir1\FileThatDoesNotExist.txt:String) [Remove-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand

Delete directory

Remove-Item command works for deleting directories as well. Below is an example command for the same.

Remove-Item C:\FolderToDelete

If the directory has files, the basic command asks for confirmation, you would need press ‘Y’ to proceed with deletion, ‘N’ for aborting it.

To avoid confirmation, you can use ‘-recurse’ argument along with ‘Remove-Item’ cmdlet. Example below.

 Remove-Item -recurse C:\FolderToDelete
0 comments… add one

Leave a Comment