How to use PowerShell to Delete a File?
Requirement: Delete a file using PowerShell
How to delete a File in PowerShell?
Deleting files is a regular task for computer users, but it can be tedious if you need to delete multiple files or from multiple folders. Fortunately, there’s an easy way to delete files in bulk using PowerShell. In this tutorial, we’ll show you how to use PowerShell to delete files quickly and effectively.
Table of contents
First, open Windows PowerShell as an administrator. To do this, type ‘PowerShell’ into your search bar and select the option “Run as Administrator”. Once the command window opens, you can use the Remove-Item
cmdlet to delete a file using PowerShell. For example, to delete a file called example.txt
in the C:\Temp
directory, you can use the following command:
Remove-Item C:\Temp\example.txt
This cmdlet will delete the specified file “C:\Temp\example.txt” from your computer. You can also use the del
alias for the Remove-Item
cmdlet to delete a file:
del C:\Temp\example.txt
Note that deleting a file is permanent and cannot be undone! It doesn’t send the file to recycle bin. You will need to have the appropriate permissions to delete a file in the specified location.
PowerShell to delete all files in a folder
If you want to delete all files from a folder, you can use the Remove-Item
cmdlet with the * wildcard character:
Remove-Item C:\Temp\*.*
This will delete all files in the C:\Temp
Directory. In case, You want to remove all files from a Folder and its sub-folders recursively, use the below script:
Get-ChildItem -Path C:\temp -File -Recurse | Remove-Item
This will delete all files in the given directory “C:\Temp” and all subdirectories.
Filter and Delete Files with PowerShell
If you want to delete multiple files of specific types (e.g., .txt or .jpg), you can use this command:
Get-ChildItem C:\Temp\*.txt -File | Remove-Item -Force
This will only remove text files from your specified folder path. Similarly, If you wish to delete files based on their timestamp, E.g., created date or last modified date, use:
Get-ChildItem C:\Temp -Recurse -File | Where {$_.LastWriteTime -lt (Get-Date).AddDays(-7)} | Remove-Item -force
This will delete all files that are not modified in the last seven days. You can delete all files created in the past seven days using the following script:
Get-ChildItem C:\Temp -Recurse -File | Where {$_.CreationTime -gt (Get-Date).AddDays(-7)} | Remove-Item -force
PowerShell to Delete a file if exists
To delete a file using PowerShell if it exists, you can use the Test-Path cmdlet to check if the file exists, and then use the Remove-Item cmdlet to delete it. Here is an example of how to delete a file named Example.txt in the specified directory “C:\Temp” if it exists:
If (Test-Path -Path "C:\Temp\Example.txt") {
Remove-Item -Path "C:\Temp\Example.txt" -Force
Write-host "File Deleted Successfully!" -f Green
}
Else {
Write-host "File doesn't exists!" -f Yellow
}
Wrapping up
Using PowerShell commands is a powerful way to quickly and safely delete multiple files at once from your computer system. The commands outlined above provide easy ways to select which files should be deleted based on their last modification time or file type. With these commands under your belt, deleting large numbers of unwanted or outdated files becomes much easier!