As a Windows system administrator, few tasks are as common as deleting files and folders. While you can certainly do deletes through File Explorer, the command line gives you precision and automation. For Windows command-line users, that means turning to PowerShell and the Remove-Item cmdlet.
Since its release in 2006, PowerShell has become the go-to automation and scripting tool for Windows. According to various surveys, usage amongst Windows admins has steadily grown over the years:
- 2008: 14%
- 2012: 33%
- 2016: 58%
- 2022: Over 75%
With its direct access to the .NET Framework and rich ecosystem of commands, PowerShell can accomplish virtually any Windows system administration task.
Let‘s dive into how we can harness the Remove-Item cmdlet to masters the art of deleting files and folders. We‘ll cover common uses cases, parameters, and best practices using hands-on examples. Whether you‘re a PowerShell novice or an expert, this guide aims to give you new insights and tools for precision deleting.
Quick Intro to PowerShell Cmdlets
For those new to PowerShell, it helps to understand what cmdlets are before we focus on Remove-Item.
Cmdlets are specialized .NET classes that implement specific system administration tasks. Their naming follows a standard verb-noun structure, like:
Get-Service(verb: Get, noun: Service)Set-Location(verb: Set, noun: Location)Start-Process(verb: Start, noun: Process)
Hundreds of cmdlets for all kinds of tasks ship built-in with PowerShell. Admins can also create custom cmdlets in any .NET language like C#.
Cmdlets typically accept various parameters that control their precise behavior. For example, Get-ChildItem lists files in a folder. Pass -Recurse to list subfolders recursively. Chaining simple cmdlets together enables accomplishing quite advanced logic.
Now let‘s dive into our cmdlet of interest – Remove-Item.
Remove-Item Explained
The Remove-Item cmdlet deletes specified files or folders. Its syntax is:
Remove-Item [-Path] <string[]> [-Filter <string>] [-Include <string[]>]
[-Exclude <string[]>] [-Recurse] [-Force] [-WhatIf] [-Confirm]
[<CommonParameters>]
The -Path parameter specifies what file or folder to delete. We can pass a single path, comma separated paths, or use wildcards for multiple paths.
Let‘s walk through common usage scenarios.
Deleting a Single File
The most basic Remove-Item usage is to delete a single file, specified by path:
Remove-Item -Path C:\Reports\March.docx
This deletes the file March.docx in the C:\Reports folder:

Deleting Multiple Files
To delete several files, we can pass multiple paths separated by commas:
Remove-Item -Path C:\Reports\March.docx, C:\Reports\April.docx, C:\Reports\May.docx
Or use wildcards like * and ? to match multiple files:
Remove-Item -Path C:\Reports\*.docx
This deletes all .docx files in C:\Reports:

Some examples of wildcard patterns:
*.txt– Deletes all.txtfiles in the pathF??.docx– Deletes all.docxfiles starting with F and 2 other letters, likeFeb.docx20??-*– Deletes all files starting with 20, two more digits, and ending with any characters
Deleting Folders
To delete an empty folder by path, use:
Remove-Item -Path C:\EmptyFolder
But to delete a folder containing files, add the -Recurse parameter:
Remove-Item -Path C:\Project\OldReports -Recurse
This will delete the OldReports folder and all its contents recursively.

-Recurse enables deleting non-empty folders. Use with caution to avoid accidentally wiping anything important!
Using Include and Exclude
The -Include and -Exclude parameters filter what gets deleted based on wildcard patterns.
For example, to selectively delete only .txt files in C:\Reports:
Remove-Item -Path C:\Reports\* -Include *.txt
Or exclude log files when cleaning up a temp folder:
Remove-Item -Path C:\Temp\*.* -Exclude *.log

This allows precise targeting of files to delete. Some more examples:
| Exclude Pattern | Description |
|---|---|
-Exclude *.log |
Exclude all .log files |
-Exclude *2019* |
Exclude files containing 2019 |
-Exclude F??,J*,MyFile.txt |
Multiple exclude filters separated by commas |
| Include Pattern | Description |
|---|---|
-Include *.docx,*.pdf |
Include multiple file types |
-Include *.tmp |
Include only .tmp files |
-Include MyFile.txt |
Include only 1 file |
We can combine include and exclude filters for advanced scenarios. For example:
Remove-Item -Path C:\Work\OldReports\* -Exclude *.xlsx -Include *.pdf,*.odt
This deletes all files excluding .xlsx, but including .pdf and .odt.
Force Deleting Read-Only Files
By default, Remove-Item will fail to delete files marked read-only. The read-only attribute is one of the file attributes in Windows. Others include Hidden and System.
To delete read-only files, use the -Force parameter:
Remove-Item -Path C:\Data\ReadOnly.txt -Force
-Force will also delete hidden and system files. Basically, it overrides any file attributes that typically prevent deletion.
Using WhatIf and Confirm
Testing cmdlets before running them in production is a good habit.
The -WhatIf switch previews what Remove-Item would delete, without actually deleting anything:
Remove-Item -Path C:\Project\* -Recurse -WhatIf
What if: Performing the operation "Remove File" on target "C:\Project\Report.docx".
What if: Performing the operation "Remove Directory" on target "C:\Project\Drafts\".
By default, Remove-Item also prompts for confirmation before actually deleting:
Confirm
Are you sure you want to perform this action?
Performing the operation "Remove File" on target "C:\MyFile.txt".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
We can bypass the prompt with -Confirm:$false, but use carefully! Accidental Remove-Item executions are harder to undo.
Handling Errors
If Remove-Item fails to delete something, it outputs an error:
Remove-Item : Could not find file ‘C:\Reports\Missing.txt‘
We can suppress errors with -ErrorAction SilentlyContinue, and check the automatic $? variable:
Remove-Item -Path C:\Missing.txt -ErrorAction SilentlyContinue
if ($?) {
# Deleted successfully
} else {
# Delete failed
}
Recursing Through Subfolders
We‘ve seen basic folder deletion with -Recurse, but there‘s more we can do:
- Specify depth with
-Depth:
# Recursively delete up to 3 folders deep
Remove-Item -Path C:\Data -Recurse -Depth 3
-
Use
-Verboseto see files/folders being deleted -
Limit scope with
-Credential
Recursing through server folders can be risky. Test carefully and use scope limitations when possible. Alternatives like Remove-Recurse from the PSDel module provide added safety.
Alternative Deletion Commands
While Remove-Item is the standard way, it‘s not the only deletion method in PowerShell:
DelandDeletealiasesRemove-Itemaliases likeri,rmdir,delRemove-Recursefrom PSDel module for safer recursive deleteClear-Itemfor clearing item content
For simple cases, Del provides a quick shortcut:
# Delete file
Del C:\Reports\old.txt
# Delete folder
Del C:\EmptyFolder
But Remove-Item gives you the most control and flexibility. The choice comes down to personal preference.
Top 10 Tips for Mastering Remove-Item
Let‘s wrap up with some key tips to help you master deletion in PowerShell:
-
Use full paths with
-Path, avoid relative paths prone to error - Lean on wildcards for deleting multiple files or folders
-
Add
-Recurseto delete non-empty folders -
Filter precisely with
-Include/-Excludeparameters -
Override attributes like read-only with
-Force -
Validate behavior first with
-WhatIf -
Set
-Confirm:$falsewith caution, or disable prompts temporarily -
Handle errors gracefully with
-ErrorAction -
Limit scope of recursive deletes with
-Depthand-Credential -
Learn alternatives like
Delfor quick one-off deletes
Whether you‘re an IT pro automating deletions or a home user cleaning up your PC, Remove-Item is a crucial tool to add to your PowerShell skillset. I hope this guide provided useful techniques enabling you to wield it like a pro. Delete carefully out there!



