How to Delete a File in PowerShell? [3 Methods]

Do you want to delete files in PowerShell? In this tutorial, I will show you various methods to delete files using PowerShell.

To delete a file in PowerShell, use the Remove-Item cmdlet followed by the -Path parameter and the file path. For instance, Remove-Item -Path "C:\path\to\file.txt" will remove the specified file. If you need to delete multiple files or require more complex conditions, you can combine Get-ChildItem with Remove-Item or use .NET methods for advanced scenarios. Always ensure to handle errors appropriately to avoid unintended data loss.

1. Using the Remove-Item Cmdlet

The primary command for deleting files in PowerShell is Remove-Item. This cmdlet is used to delete various types of items, including files, directories, registry keys, and more.

Here’s a basic example of how to delete a single file:

Remove-Item -Path "C:\MyFolder\file.txt"

This command will delete the file specified in the path. If the file does not exist, PowerShell will throw an error.

Delete Multiple Files in PowerShell

To delete multiple files in PowerShell, you can use wildcards (*) to match multiple files. For instance, to delete all .txt files in a directory:

Here is the cmdlet.

Remove-Item -Path "C:\MyFolder\*.txt"

Here you can see below I have in the folder there are multiple .txt files.

Delete a File in PowerShell

Now, after I ran the above PowerShell script using VS code, it deleted all the .txt files from the folder; check the screenshot below:

Delete Multiple Files in PowerShell

Delete Files with Confirmation in PowerShell

If you want to avoid accidental deletions of files, you can add the -Confirm parameter, which will prompt you before deleting each file:

Remove-Item -Path "C:\MyFolder\file.txt" -Confirm

Force Delete Read-Only Files in PowerShell

Sometimes, you may encounter read-only files that cannot be deleted without additional permissions. To delete such files, use the -Force parameter in PowerShell:

Remove-Item -Path "C:\MyFolder\readonlyfile.txt" -Force

Delete Files Recursively in PowerShell

To delete files and subfolders within a directory recursively, you can use the -Recurse parameter. This will delete both the files and subfolders within the specified path:

Remove-Item -Path "C:\MyFolder\directory" -Recurse

2. Using Get-ChildItem with Remove-Item

For more control over which files to delete, you can combine Get-ChildItem with Remove-Item. This approach allows you to filter and select files before deletion.

Deleting Files Based on a Condition

For example, if you want to delete all files larger than 1MB, you can use the following PowerShell script:

Get-ChildItem -Path "C:\path\to\your\directory" -File | Where-Object { $_.Length -gt 1MB } | Remove-Item

Deleting Files Older Than a Certain Date

To delete files that are older than 30 days, you could use the below PowerShell script:

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

3. Using the .NET Method

PowerShell also allows you to tap into .NET Framework’s capabilities to delete files. This method offers more advanced options and can be useful in certain scenarios.

Here’s an example of using the .NET method to delete a file:

[System.IO.File]::Delete("C:\MyFolder\file.txt")

This line of code directly calls the Delete method from the System.IO.File class to remove the specified file.

Error Handling When Deleting Files Using PowerShell

You should always use an error-handling mechanism while deleting files using PowerShell. You can use Try, Catch, and Finally blocks to manage exceptions that may occur during deletion.

Here’s an example of error handling in a file deletion script in PowerShell:

try {
    Remove-Item -Path "C:\MyFolder\file.txt" -ErrorAction Stop
} catch {
    Write-Host "An error occurred: $_"
} finally {
    Write-Host "Operation completed."
}

This script attempts to delete a file and stops if an error occurs, catching the exception and printing an error message. The finally block ensures that the script prints “Operation completed.” regardless of the outcome.

Conclusion

Deleting files using PowerShell is a straightforward process that can be performed using various methods, including the Remove-Item cmdlet, combining Get-ChildItem with Remove-Item, or using the .NET Framework.

In this PowerShell tutorial, I have explained how to delete a file in PowerShell using various methods.

You may also like: