PowerShell Delete File: Commands, Examples, and Safety
To delete a file in PowerShell, use Remove-Item followed by the file path, for example Remove-Item -Path '.\notes.txt'. PowerShell removes the file from the filesystem; check the path carefully first, because deletion may not send it to the Recycle Bin and may be difficult to undo.
Basic command and examples
Remove-Item is PowerShell’s standard command for removing files and other items, including folders and registry entries. For a file in the current directory, enter:
Remove-Item -Path '.\notes.txt'
Use a full path when the file is elsewhere. Quote paths containing spaces:
Remove-Item -Path 'C:\Users\Alex\Documents\old draft.txt'
The -Path parameter accepts a path that can include wildcard characters. For example, this removes matching text files from a particular folder:
Remove-Item -Path 'C:\Temp\*.tmp'
That wildcard applies to names in the specified folder; it does not automatically search every subfolder. For recursive removal, see the section below. In an interactive session, PowerShell may ask for confirmation in some situations, particularly when a command is potentially destructive. Do not assume that a prompt will always appear.
Use the right path and verify the target
Paths are interpreted in the context of the current PowerShell location unless they are absolute. Check that location with Get-Location, or list the intended directory with Get-ChildItem before deleting anything. A relative path such as . refers to the current directory;
otes.txt.. refers to its parent.
otes.txt
For a safer first pass, inspect what a wildcard matches:
Get-ChildItem -Path 'C:\Temp' -Filter '*.tmp'
Then use the matching path in the removal command. You can also ask PowerShell to show the proposed action without carrying it out:
Remove-Item -Path 'C:\Temp\*.tmp' -WhatIf
-WhatIf is useful for reviewing the effect of many PowerShell commands, but it is a preview, not a backup or a guarantee that every later run will have identical results. Files may change between the preview and the actual command.
Delete a folder and its contents
To remove a directory that contains files or other directories, use -Recurse:
Remove-Item -Path 'C:\Temp\OldProject' -Recurse
Recursive deletion can remove a large tree of data. Confirm the exact directory and consider a preview before running it:
Remove-Item -Path 'C:\Temp\OldProject' -Recurse -WhatIf
Do not add -Force casually. It can allow removal of items marked read-only or hidden, but it does not override every restriction, such as insufficient permissions or a file being in use. Combine it with recursion only when you understand the target and need those behaviors:
Remove-Item -Path 'C:\Temp\OldProject' -Recurse -Force
There is an important distinction between deleting a folder and deleting only its contents. The command above targets the folder itself as well as its descendants. If you intend to keep the folder, specify the contents, inspect the result, and be especially cautious with wildcards:
Remove-Item -Path 'C:\Temp\OldProject\*' -Recurse -WhatIf
Delete files by filter or condition
For a name pattern, pass a wildcard path to Remove-Item. To filter by properties such as extension or age, first retrieve matching files with Get-ChildItem, then pass those objects to Remove-Item. For example, this previews deletion of .log files in one folder that were last modified more than 30 days ago:
Get-ChildItem -Path 'C:\Logs' -File -Filter '*.log' |
Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-30) } |
Remove-Item -WhatIf
After verifying the preview, remove -WhatIf to perform the action. This example does not search subfolders; add -Recurse to Get-ChildItem if a recursive search is intended. Filtering the results before removal helps avoid relying on an overly broad wildcard.
What the options mean
-Pathidentifies the item to remove and supports wildcard patterns.-LiteralPathtreats the supplied path exactly as written, rather than interpreting wildcard characters in it. Use it when a filename itself contains characters such as[or].-Recurseincludes nested items when removing a directory.-Forcerequests removal of items that are normally hidden or read-only where supported; it is not an administrator-permission bypass.-WhatIfpreviews the action instead of performing it.-Confirmrequests confirmation before an action. It can be useful for an interactive command, but does not replace checking the path.
For example, to delete a file whose name contains wildcard-like brackets, use its literal path:
Remove-Item -LiteralPath 'C:\Temp\report[old].txt'
Common problems and recovery
If PowerShell says the path cannot be found, check spelling, drive, current directory, and whether the file has already been moved or deleted. Use Test-Path -LiteralPath 'C:\Temp\notes.txt' to check whether a specific path currently exists. If access is denied, verify your account’s permissions and whether the item is protected; running PowerShell as an administrator should be reserved for cases where elevated access is genuinely appropriate.
A file-in-use error can mean another process has an open handle. Close the application using the file and try again; forcing removal does not reliably solve a file lock. If a file is read-only, -Force may help, but first make sure the attribute is not protecting data you still need.
PowerShell’s Remove-Item generally deletes directly rather than moving items to the Recycle Bin. Recovery depends on the storage device, filesystem, backups, and what happened after deletion; there is no guaranteed undo command. If recovery matters, stop writing to the affected drive and use a suitable backup or recovery process rather than continuing to modify it.
Frequently Asked Questions
How do I delete a file in PowerShell without a prompt?
Use Remove-Item -Path 'C:\path\file.txt'. If a confirmation prompt appears for a particular operation, review it before responding; avoid suppressing prompts broadly when the target is uncertain.
Does Remove-Item send files to the Recycle Bin?
Usually, no. Treat Remove-Item as direct deletion, not as a Recycle Bin operation, and keep a backup if the file may be needed.
How do I delete all files with an extension?
Use a specific folder and a pattern, such as Remove-Item -Path 'C:\Temp\*.log'. Preview the matches first with Get-ChildItem or use -WhatIf.
How do I delete a file whose name includes brackets?
Use -LiteralPath, for example Remove-Item -LiteralPath 'C:\Temp\file[1].txt'. This prevents wildcard interpretation.
What is the difference between Remove-Item and del?Remove-Item is the PowerShell cmdlet. In PowerShell, del is an alias for it, so the same caution about paths and permanent deletion applies.
Can I recover a file deleted with PowerShell?
There is no reliable PowerShell undo for a completed deletion. Check backups or recovery options promptly; chances depend on the device and whether its storage has since been overwritten.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.