PowerShell Remove-Item Cmdlet [With Examples]

If you’re new to PowerShell and want to learn how to delete files, folders, or other items safely and efficiently, then you should learn about the PowerShell Remove-Item cmdlet. In this tutorial, you’ll learn what Remove-Item is, how to use it, the syntax, and see practical examples.

What is the PowerShell Remove-Item Cmdlet?

The Remove-Item cmdlet in PowerShell is used to delete items from various locations, such as files, folders, registry keys, variables, and more.

It’s a powerful command that should be used carefully, as deleted items are often not recoverable through the Recycle Bin. With the right parameters, you can control what gets deleted and how.

Syntax of Remove-Item

The basic syntax for PowerShell Remove-Item is:

Remove-Item -Path <path> [-Parameter]

Common Parameters:

  • -Path: Specifies the path to the item(s) to remove.
  • -Recurse: Deletes all items in the specified location, including subfolders and files.
  • -Force: Forces the removal of items that cannot otherwise be changed, such as hidden or read-only files.
  • -Confirm: Prompts for confirmation before deleting each item.
  • -WhatIf: Shows what would happen if the command runs, but does not actually delete anything.

Check out Get Files Older Than 1 Month with PowerShell

Important Notes Before Using Remove-Item

  • Deleted files are usually not sent to the Recycle Bin. Once removed, they are typically unrecoverable.
  • Always double-check your path and parameters. Mistakes can result in loss of important data.
  • Use -WhatIf for a dry run. This helps you see what will be deleted without actually deleting anything.

Read Delete User Profiles Older Than 30 Days Using PowerShell

PowerShell Remove-Item Examples

Let’s look at some practical examples to understand how Remove-Item in PowerShell works in different scenarios.

Example 1: Delete a Single File

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

This command will permanently delete the file named oldfile.txt located in the C:\Temp folder. It’s a straightforward way to remove a file you no longer need. Be careful, as the file is immediately deleted and does not go to the Recycle Bin.

Example 2: Delete All Files in a Folder

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

Here, the asterisk (*) is a wildcard that matches all files ending with .log in the C:\Logs directory. This command is useful for cleaning up old log files in bulk without having to delete each one manually. Only files are deleted—subfolders remain untouched.

Example 3: Delete a Folder and Its Contents

Remove-Item -Path "C:\OldBackups" -Recurse

Using the -Recurse parameter tells PowerShell to delete not just the OldBackups folder, but also everything inside it, including all subfolders and files. This is helpful for removing entire directory structures in one go.

Warning: This action is irreversible, so use it with caution.

Check out Create a Folder with the Current Date using PowerShell

Example 4: Force Delete Hidden or Read-Only Files

Remove-Item -Path "C:\Temp\hiddenfile.txt" -Force

Some files are hidden or marked as read-only and cannot be deleted with standard commands. The -Force parameter overrides these restrictions, allowing you to delete even protected files. This is especially useful when cleaning up stubborn files that refuse to be deleted otherwise.

Example 5: Confirm Before Deleting

Remove-Item -Path "C:\Temp\*.tmp" -Confirm

Adding the -Confirm parameter will prompt you for confirmation before each .tmp file is deleted from the C:\Temp folder. This is a great safety feature when you want to double-check each deletion and avoid accidental data loss.

Example 6: WhatIf – See What Would Happen

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

The -WhatIf parameter allows you to preview what would be deleted by the command without actually removing anything. PowerShell will list all the files and folders that would be affected. This is highly recommended for testing your commands before running them for real, especially when dealing with large or important directories.

Read Remove the First and Last Lines from a File Using PowerShell

PowerShell Remove-Item Cmdlet

Remove-Item with Registry Keys

Remove-Item can also be used to delete registry keys, not just files and folders.

Remove-Item -Path "HKCU:\Software\OldApp"

This command removes the OldApp registry key from the current user’s registry hive. Modifying the registry can have significant effects on your system, so always back up your registry and ensure you are deleting the correct key.

Remove-Item with Variables and Aliases

You can even use Remove-Item to delete variables or aliases from your PowerShell session.

$myVar = "Hello"
Remove-Item -Path Variable:myVar

In this example, we first create a variable called $myVar. The Remove-Item command then deletes this variable from the session, freeing up the name for reuse or simply cleaning up your environment.

Check out Find and Remove Empty Folders Using PowerShell

Best Practices When Using Remove-Item

  • Test first with -WhatIf. This helps prevent accidental deletions.
  • Use absolute paths to avoid deleting the wrong files or folders.
  • Be careful with -Recurse and wildcards; they can delete large numbers of files quickly.
  • Always have backups of important data before running delete commands.

Common Errors and Troubleshooting

  • Access Denied: You may need to run PowerShell as Administrator or use -Force to remove certain files.
  • Path Not Found: Double-check your file or folder path for typos or missing directories.
  • File in Use: Ensure the file is not open in another program or process before deleting.

Summary

The Remove-Item cmdlet in PowerShell is used to delete files, folders, registry keys, and other items. Always use caution, especially with recursive deletions and wildcards, and take advantage of the -WhatIf parameter to preview actions before executing them. I hope you now understand how to work with the PowerShell Remove-Item cmdlet.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.