keepcalculator

06 Numbers and Powers

Delete Files and Folders with PowerShell

Card No. 06·87915 · Updated · Standard formula, shown below

Yes, you can delete files and folders with PowerShell using the Remove-Item cmdlet, which is the direct equivalent of the Windows del and rmdir commands but with more power and flexibility. You can delete a single file, multiple files matching a pattern, an entire folder tree, or even files on remote machines, all with a few lines of script.

Why Use PowerShell Instead of File Explorer?

PowerShell gives you precise control over deletion that the graphical interface cannot match. You can filter files by extension, age, size, or name pattern; you can delete files across hundreds of folders in one command; and you can automate the process in scripts. File Explorer is fine for occasional manual deletions, but when you need to clean up temporary files, remove old logs, or clear out a build directory, PowerShell turns a repetitive task into a single line.

The Core Cmdlet: Remove-Item

The heart of file deletion in PowerShell is Remove-Item. Its basic syntax is simple:

Remove-Item -Path "C:\Temp\oldfile.txt"

This deletes the specified file. To delete a folder and everything inside it, you add the -Recurse switch:

Remove-Item -Path "C:\Temp\OldFolder" -Recurse

Without -Recurse, PowerShell will prompt you to confirm if the folder is not empty. With it, the folder and all its contents are removed in one go. You can also use wildcards to delete multiple items:

Remove-Item -Path "C:\Logs\*.log"

This deletes every .log file in the C:\Logs folder but leaves subfolders untouched.

Deleting Folders: Remove-Item -Recurse

When you delete a folder, you have two choices: delete just the empty folder (which fails if it has contents), or delete the folder and everything inside it. The -Recurse parameter handles the latter. For example, to remove the entire C:\Temp\Project directory tree:

Remove-Item -Path "C:\Temp\Project" -Recurse -Force

The -Force parameter suppresses confirmation prompts and also deletes hidden or read-only files. Without -Force, PowerShell may ask you to confirm each item or skip read-only files. If you are running this in a script, you almost always want both -Recurse and -Force.

Deleting Files with Filters and Conditions

Often you do not want to delete everything, only files that match certain criteria. PowerShell makes this easy by combining Get-ChildItem with Remove-Item. For instance, to delete all files older than 30 days in a specific folder:

Get-ChildItem -Path "C:\Logs" -File | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-30) } | Remove-Item

Here, Get-ChildItem lists the files, Where-Object filters them by the LastWriteTime property, and the results are piped to Remove-Item. You can filter by size, extension, name pattern, or any file property. For example, to delete all files larger than 100 MB:

Get-ChildItem -Path "C:\Temp" -File | Where-Object { $_.Length -gt 100MB } | Remove-Item

Note that -File is a parameter of Get-ChildItem that restricts the output to files only, excluding directories. This prevents accidentally trying to delete folders when you only want files.

Common Mistakes and How to Avoid Them

Deleting files is irreversible, so it is critical to avoid common pitfalls:

  • Missing -Recurse: When deleting a non-empty folder without -Recurse, PowerShell will prompt you. In a script, this prompt can hang the script or cause an error. Always include -Recurse when you intend to delete the entire folder tree.
  • Wildcard surprises: A pattern like C:\Temp\* matches all items directly inside C:\Temp, but not subfolders’ contents. To delete everything inside a folder including subfolders, use C:\Temp\* -Recurse or better, delete the folder itself with -Recurse.
  • Path typos: A wrong path can delete the wrong folder. Always double-check the path. You can use Test-Path to verify it exists before deletion.
  • Read-only files: Without -Force, read-only files cause errors. If you expect read-only files, add -Force.
  • Piping without -File: If you pipe Get-ChildItem without -File to Remove-Item, it will also try to delete folders, which may not be what you want.

To test what a deletion command would do without actually deleting, you can use the -WhatIf parameter:

Remove-Item -Path "C:\Temp\Project" -Recurse -WhatIf

This shows you what would be deleted without deleting anything. It is an excellent safety net.

Deleting to the Recycle Bin

By default, Remove-Item permanently deletes files, bypassing the Recycle Bin. If you want to send files to the Recycle Bin instead, you need to use the Microsoft.VisualBasic.FileIO.FileSystem class:

Add-Type -AssemblyName Microsoft.VisualBasic
[Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile("C:\Temp\oldfile.txt", 'OnlyErrorDialogs', 'SendToRecycleBin')

For folders, use DeleteDirectory with similar arguments. This is a common requirement for users who want a safety net. Note that this method is slower than Remove-Item but safer.

Deleting Files on Remote Computers

PowerShell can delete files on remote machines using Invoke-Command or by mapping a network drive. For example:

Invoke-Command -ComputerName "Server01" -ScriptBlock { Remove-Item -Path "D:\Temp\*.tmp" -Force }

This requires PowerShell remoting to be enabled and appropriate permissions. Alternatively, you can use a UNC path directly with Remove-Item if the share allows it:

Remove-Item -Path "\\Server01\Share\Temp\*.tmp" -Force

This is simpler but may be slower and less reliable for large trees.

Frequently Asked Questions

How do I delete all files in a folder but keep the folder itself?

Use Remove-Item -Path "C:\Folder\*" -Force to delete all items directly inside the folder, including subfolders if you add -Recurse. To delete only files and keep subfolders, use Get-ChildItem -Path "C:\Folder" -File | Remove-Item.

What is the difference between Remove-Item and del?

del is an alias for Remove-Item in PowerShell. They are the same command. However, Remove-Item is the full cmdlet name and is preferred in scripts for clarity. The old Command Prompt del is different, but in PowerShell both work.

Can I undo a deletion with PowerShell?

No, Remove-Item permanently deletes files unless you specifically use the Recycle Bin method described above. There is no built-in undo. Always test with -WhatIf and consider using the Recycle Bin for important files.

Why does Remove-Item prompt me to confirm?

PowerShell prompts when you delete a non-empty folder without -Recurse, or when you delete items that are read-only, hidden, or have other attributes. Adding -Recurse and -Force suppresses most prompts.

How do I delete files older than X days?

Use the pipeline: Get-ChildItem -Path "C:\Folder" -File | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-7) } | Remove-Item. Change the number of days and the path as needed.

Is it safe to delete a folder with -Recurse -Force?

It is safe only if you are certain the folder contains nothing you need. The combination removes everything without confirmation. Always double-check the path and use -WhatIf first to see what will be deleted.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →