≡ Menu

Powershell: How To Delete Files

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

Delete file from PowerShell

We can delete a file using remove-item command. Syntax given below.

remove-tem 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
0 comments… add one

Leave a Comment