Remove Item - PowerShell
Overview
Remove-Item is a PowerShell command that deletes specified items from the file system, including files, directories, and symbolic links. It provides a convenient and versatile way to manage and clean up resources in your system.
Syntax
Remove-Item [-Path] <string[]> [-Recurse] [-Force] [-Confirm] [-WhatIf]
[-Verbose] [-ErrorAction <Action>]
Options/Flags
- -Path: Specifies the path(s) to the item(s) to be deleted. This parameter is mandatory.
- -Recurse: Recursively removes items from subdirectories.
- -Force: Suppresses confirmation prompts and forces deletion of read-only or protected items.
- -Confirm: Prompts for confirmation before deleting each item.
- -WhatIf: Simulates the operation without actually deleting any items.
- -Verbose: Displays detailed information about the deletion process.
- -ErrorAction: Specifies the action to be taken if errors occur during deletion.
Examples
Simple file deletion:
Remove-Item c:\temp\myfile.txt
Deleting multiple files:
Remove-Item c:\temp\file1.txt c:\temp\file2.txt
Recursive folder deletion:
Remove-Item -Path c:\oldfolder -Recurse
Forcefully deleting protected items:
Remove-Item -Path c:\system32\myfile.exe -Force
Common Issues
- Access denied errors: Ensure that you have sufficient permissions to delete the specified items.
- Item not found errors: Verify that the path to the item is correct.
Integration
Combining with other commands:
Get-ChildItem c:\temp | Remove-Item -Recurse
Using in scripts:
$itemsToRemove = Get-ChildItem c:\temp | Where-Object {$_.Extension -eq ".tmp"}
Remove-Item -Path $itemsToRemove
Related Commands
- New-Item
- Get-ChildItem
- Get-Content