How to Use Remove-Item PowerShell cmdlet to Delete Files

4 min read | Published On - May 30, 2025
Track File and Folder Deletion with Lepide File Server Auditor
x

Management of files and directories is an everyday task in computing, particularly in the Windows PowerShell environment. However, the deletion of files and cleaning up of folders can be a time-consuming task, especially when you need to perform the same task regularly on multiple computers. However, by using PowerShell, you can create scripts that will delete a file quickly and efficiently by using the Remove-Item cmdlet.

PowerShell allows you to delete a single file, an entire directory or a group of files based on specific criteria. Using PowerShell, it’s possible to only delete a file if it exists, files that are older than x number of days, or delete files based on a wildcard pattern using just a few PowerShell commands.

In this article we will look at how to use PowerShell to delete files and then look at how Lepide File Server Auditor helps with tracking files and folders which have been deleted.

How to Use PowerShell to Delete Files?

The Remove-Item cmdlet in PowerShell is used for deleting files and directories, as well as other types of items in PowerShell such as local file systems as well as other PowerShell providers like the Registry, Certificate Store, and Environment variables. Remove-Item can also accept input from other cmdlets through the pipeline, enabling more dynamic operations and automation of file management tasks.

It is important to note that by default, items deleted with Remove-Item are permanently removed from the system and are not sent to the Recycle Bin. It is, therefore, important to be certain about actions when using this cmdlet.

Below are some examples of where you might want to use the Remove-Item command to delete files:

  • Removing Old Backups: Deleting old backup files helps to save space.
  • Deleting Temporary Files: Regularly removing temporary files helps to maintain system performance.
  • Batch Deletion: Removing multiple files with a specific extension is time saving and efficient.
  • Removing Empty Directories: Removing empty folders in a directory structure helps to keep things organized.
  • Registry Key Cleanup: Deleting specific registry keys that are no longer needed

The basic syntax for the Remove-Item cmdlet is as follows:

Remove-Item [-Path] [-Force] [-Recurse] [-LiteralPath ] [-Confirm] [-WhatIf] [-ErrorAction ] [-WarningAction ] [-InformationAction ] [-InformationVariable ] []

Below are some examples of how to use the Remove-Item cmdlet

To delete a single file: To delete a file called accounts.txt the command would be:

Remove-Item -Path "C:\Training\accounts.txt"

To delete a specific folder: To delete a folder named “OldExamples” located in “C:\training, the command would be.

Remove-Item -Path "C:\Training\OldExamples"

To delete a folder and all its contents: To delete a folder and all its contents, you will need to use the “-Recurse” parameter to make sure that the folder, together with all its files and subfolders, is deleted. If this parameter is not included, the cmdlet will not delete the folder if it contains files or other folders. You can use the cmdlet as follows:

Remove-Item -Path "C:\Trainng\OldExamples" -Recurse

To delete multiple files within a directory: If you want to delete all files of a specific type within a directory, you can use the asterisk (*) wildcard with the file extension. For example, to delete all “.txt” files in a specific folder, you can use the following:
Remove-Item -Path "C:\Training\OldExamples\*.txt"

How Lepide File Server Auditor helps in Tracking File and Folder Deletion

Lepide Auditor for File Server reports on changes happening to your key systems and data and it helps to reduce your threat surface area, detect threats and meet compliance demands. The Lepide’s File Server auditing tool includes many pre-defined reports to provide visibility enabling you to identify threats, reduce risk and mitigate damage. One of these reports is the Files and Folders Deleted Report and an example of this is shown below: file an folder deletion report - Lepide

To run this report:

  • Select Lepide Auditor, Reports
  • From here, expand File Server
  • Select Files and Folders Deleted
  • Specify a date range if required or leave as the default of today
  • Select Generate Report

The report is generated and can be filtered, sorted and exported to CSV and PDF formats.

See how Lepide File Server Auditing solution works
x
Learn More...

Track File and Folder Deletion with Lepide File Server Auditor

x
Learn More...