- You want to remove a file with Remove-Item.
- You want to ignore errors even if the file doesn’t exist.
You get an error when execute Remove-Item -Force in the file that does not exist.
The behavior is by design.
- rm command with -f option in Unix-like systems doesn’t prompt before removing files and returns 0 (success) even if a target file doesn’t exist.
- Remove-Item with -Force option in PowerShell doesn’t prompt before removing files, but returns error if a target file doesn’t exist.
Remove-Item cmdlet doesn’t have a option to ignore such an error. So, you have to work around:
- Check the existence of a file with Test-Path before execute Remove-Item. This workaround is not strictly safe because of TOCTTOU problem.
- Use Remove-Item -Force with -ErrorAction SilentlyContinue option. This workaround is also not strictly safe because it ignore all other errors too.